2017-10-23 13 views
-1

C++ Serverで動作し、サーバーからJSONオブジェクトを受け取りたいJavaアプリケーションを作成しています。基本的に私はLatLng(Latitude、Longitude)をサーバーに送信していますが、Google Maps APIから要素を返していますが、戻り要素にエスケープ文字が含まれているため、JSONObjectで解析できません。 (:org.json.JSONException:文字579でまだ終了していないオブジェクトのエラー)あなたがでJSON文字列からオブジェクトを作成することができますJava JSONリテラル文字列を解析します。

{ 
"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`[email protected]?eA\\", 
    "middle":"}|ibGi`[email protected][email protected]@MVGMwB`@@[email protected]@[email protected]|@[email protected]@{[email protected]_B^}[email protected]@[email protected][email protected]\\", 
    "start":"[email protected]@[email protected]_A" 
} 
} 
+0

ポリラインオブジェクトでパーサが "\\"で失敗する可能性があります。文字列をエスケープしようとするか、またはパーサーが失敗するような文字の組み合わせをすべて置き換えてください。 –

+0

最も良い方法は、サーバー側から\をエスケープしてクライアント側で正しく読み取れるようにすることです。 –

+0

ポリラインオブジェクトなしでJSONを解析しようとしましたが、成功しました。私もoutput.replace( "//"、 "////")を試しましたが、成功はありません – GhoSTBG

答えて

-1

http://www.jsonschema2pojo.org/

@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オブジェクトの

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

あなたは例のオブジェクトのすべてを取得します。

私はあなたの問題に役立つことを願っています!

+0

これは機能しません。問題は、文字列にエスケープ文字が含まれているため、新しいJSONObjectを作成できないことです – GhoSTBG

関連する問題