简体   繁体   中英

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. 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.

@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)

{  
"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 :

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;
}

}

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.

I hope it will help your problem!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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