我正在制作与C ++服务器一起运行并希望从服务器接收JSON对象的Java应用程序。基本上,我将LatLng(Latitude,Longtitude)发送到服务器并且它从Google Maps API返回我的元素,但问题是返回元素中包含转义字符,我可以'用JSONObject解析它们。
@Override
protected void onPostExecute(string output) {
super.onPostExecute(output);
try {
JSONObject data = new JSONObject(output);
String test = data.getJSONObject("main_Points").getString("name");
System.out.print(test);
} catch(Exception ex) {
ex.printStackTrace();
}
}
这是我的JSON对象(错误:org.json.JSONException:字符579处未终止的对象)
{
"main_Points":{
"final":{
"lat":42.502812,
"lng":27.4691601,
"name":"бул. „Сан Стефано“ 62, 8001 ж.к. Братя Миладинови, Бургас, България"
},
"start":{
"lat":42.4912504,
"lng":27.4725683,
"name":"бул. „Иван Вазов“ 1, 8000 Бургас Център, Бургас, България"
}
},
"nearestPoints":{
"final":{
"lat":42.503294,
"lng":27.468482,
"name":"БСУ"
},
"start":{
"lat":42.490604,
"lng":27.474128,
"name":"Автогара Юг"
}
},
"polylines":{
"final":"qilbGgatfDR`Bo@?eA\\",
"middle":"}|ibGi`ufD@~@Ah@MVGMwB`@@bCG~CeFtBiBr@yApAeBl@sBr@aDbAiC|@cCr@uJfCaDz@{ElAoCl@OC_B^}Bh@Gm@m@cF_A@eA\\",
"start":"iajbGqvtfDAaDvBa@FLLW@i@A_A"
}
}
答案 0 :(得分:-1)
您可以使用以下命令从json字符串创建对象:
http://www.jsonschema2pojo.org/
-----------------------------------com.example.Example.java-----------------------------------
package com.example;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Example {
@SerializedName("main_Points")
@Expose
private MainPoints mainPoints;
@SerializedName("nearestPoints")
@Expose
private NearestPoints nearestPoints;
@SerializedName("polylines")
@Expose
private Polylines polylines;
public MainPoints getMainPoints() {
return mainPoints;
}
public void setMainPoints(MainPoints mainPoints) {
this.mainPoints = mainPoints;
}
public NearestPoints getNearestPoints() {
return nearestPoints;
}
public void setNearestPoints(NearestPoints nearestPoints) {
this.nearestPoints = nearestPoints;
}
public Polylines getPolylines() {
return polylines;
}
public void setPolylines(Polylines polylines) {
this.polylines = polylines;
}
}
----------------------------------- com.example.Final.java ----- ------------------------------
package com.example;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Final {
@SerializedName("lat")
@Expose
private Double lat;
@SerializedName("lng")
@Expose
private Double lng;
@SerializedName("name")
@Expose
private String name;
public Double getLat() {
return lat;
}
public void setLat(Double lat) {
this.lat = lat;
}
public Double getLng() {
return lng;
}
public void setLng(Double lng) {
this.lng = lng;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
----------------------------------- com.example.Final_.java ----- ------------------------------
package com.example;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Final_ {
@SerializedName("lat")
@Expose
private Double lat;
@SerializedName("lng")
@Expose
private Double lng;
@SerializedName("name")
@Expose
private String name;
public Double getLat() {
return lat;
}
public void setLat(Double lat) {
this.lat = lat;
}
public Double getLng() {
return lng;
}
public void setLng(Double lng) {
this.lng = lng;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
----------------------------------- com.example.MainPoints.java ----- ------------------------------
package com.example;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class MainPoints {
@SerializedName("final")
@Expose
private Final _final;
@SerializedName("start")
@Expose
private Start start;
public Final getFinal() {
return _final;
}
public void setFinal(Final _final) {
this._final = _final;
}
public Start getStart() {
return start;
}
public void setStart(Start start) {
this.start = start;
}
}
----------------------------------- com.example.NearestPoints.java ----- ------------------------------
package com.example;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class NearestPoints {
@SerializedName("final")
@Expose
private Final_ _final;
@SerializedName("start")
@Expose
private Start_ start;
public Final_ getFinal() {
return _final;
}
public void setFinal(Final_ _final) {
this._final = _final;
}
public Start_ getStart() {
return start;
}
public void setStart(Start_ start) {
this.start = start;
}
}
----------------------------------- com.example.Polylines.java ----- ------------------------------
package com.example;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Polylines {
@SerializedName("final")
@Expose
private String _final;
@SerializedName("middle")
@Expose
private String middle;
@SerializedName("start")
@Expose
private String start;
public String getFinal() {
return _final;
}
public void setFinal(String _final) {
this._final = _final;
}
public String getMiddle() {
return middle;
}
public void setMiddle(String middle) {
this.middle = middle;
}
public String getStart() {
return start;
}
public void setStart(String start) {
this.start = start;
}
}
----------------------------------- com.example.Start.java ----- ------------------------------
package com.example;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Start {
@SerializedName("lat")
@Expose
private Double lat;
@SerializedName("lng")
@Expose
private Double lng;
@SerializedName("name")
@Expose
private String name;
public Double getLat() {
return lat;
}
public void setLat(Double lat) {
this.lat = lat;
}
public Double getLng() {
return lng;
}
public void setLng(Double lng) {
this.lng = lng;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
----------------------------------- com.example.Start_.java ----- ------------------------------
package com.example;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Start_ {
@SerializedName("lat")
@Expose
private Double lat;
@SerializedName("lng")
@Expose
private Double lng;
@SerializedName("name")
@Expose
private String name;
public Double getLat() {
return lat;
}
public void setLat(Double lat) {
this.lat = lat;
}
public Double getLng() {
return lng;
}
public void setLng(Double lng) {
this.lng = lng;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
并且
@Override
protected void onPostExecute(string output) {
super.onPostExecute(output);
try {
JSONObject data = new JSONObject(output);
Gson g = new Gson();
Example example = g.fromJson(data.toString(), Example.class);
} catch(Exception ex) {
ex.printStackTrace();
}
}
您将获得Example对象中的所有内容。
我希望它能帮到你解决问题!