简体   繁体   English

Java JSON 解析文字字符串

[英]Java JSON Parse literal string

I'm making Java Application which runs with C++ Server and want to receive a JSON Object from the server.我正在制作与 C++ 服务器一起运行的 Java 应用程序,并希望从服务器接收 JSON 对象。 Basiclly I'm sending LatLng(Latitude, Longtitude) to the server and it's returning me elements from the Google Maps API, but the problem is that the return elements have escape characters in them and I can't parse them with JSONObject.基本上我将 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();
    }
}

And here's my JSON Object (Error:org.json.JSONException: Unterminated object at character 579)这是我的 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"
}
}

You can create object from json string with :您可以使用以下 json 字符串创建对象:

http://www.jsonschema2pojo.org/ 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----------------------------------- --------------------- 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----------------------------------- ----------------------------------- 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----------------------------------- ------------------------------------- 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----------------------------------- ----------------------------------- 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----------------------------------- ----------------------------------- 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----------------------------------- ------------------------------------- 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----------------------------------- --------------------- 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;
}

}

And

 @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();
        }
    }

You will get everything in Example object.您将获得 Example 对象中的所有内容。

I hope it will help your problem!我希望它能帮助您解决问题!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM