2012-12-19 6 views
5

Ich habe ein Problem mit Json. Ich möchte meine Json aussehen:Json in einem Webdienst überprüfen

data={"phoneId":1,"token":"APA91bF2tN5g1TtULFE5tysRMAarygjX4w9hjTGCqT3SL-PwiMV6aqTtkV3lpqLkc7msVfEdTnyd_pJVFNMM_fjEbeVSuCjiNPVKx7p9sYC1DoWnuKUurt31E1yh2RDwl_oprfKxEF18PP6Q8dXHZe6FeflE3CIxBg","appId":5} 

Dies ist mein Beitrag zu Web-Service:

JSONObject jsonObject = new JSONObject(); 
        jsonObject.put("token", regId); 
        jsonObject.put("appId", GlobalConfig.getAPPLICATION_ID()); 
        jsonObject.put("phoneId", 1); 

        JSONArray jArrayParam = new JSONArray(); 
        jArrayParam.put(jsonObject); 

        JSONObject finaljsonobj = new JSONObject(); 

        finaljsonobj.put("data", jArrayParam); 

       System.out.println(jsonObject.toString()); 
       List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(); 
       nameValuePair.add(new BasicNameValuePair("Token",jArrayParam.toString())); 

       HttpClient httpclient = new DefaultHttpClient(); 
       HttpPost httppost = new HttpPost(GlobalConfig.getSendEmail()); 
        httppost.addHeader("Authorization", "Basic " + Base64.encodeToString(
          (GlobalConfig.getAuthString()).getBytes(),Base64.NO_WRAP)); 
       httppost.setEntity(new UrlEncodedFormEntity(nameValuePair, HTTP.UTF_8)); 

       // Execute HTTP Post Request 
       HttpResponse response = httpclient.execute(httppost); 

Wie kann ich überprüfen, ob mein json wie in meinem Beispiel sieht? Ich möchte überprüfen, wie mein JSON Post zum Webservice ist.

Antwort

2

versuchen wie:

JSONObject jsonObject = new JSONObject(); 
jsonObject.put("token", regId); 
jsonObject.put("appId", GlobalConfig.getAPPLICATION_ID()); 
jsonObject.put("phoneId", 1); 

System.out.println("data="+jsonObject.toString()); 
List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(); 
nameValuePair.add(new BasicNameValuePair("Token","data="+jsonObject.toString())); 

dies erstellen Sie Ihre JSON-Objekt als:

data={ 
    "phoneId": 1, 
    "token": "token", 
    "appId": 5 
} 
0

Ich denke, dass Ihre finaljsonobj die Struktur haben:

{"data":[{"phoneId":1,"token":"APA91bF2tN5g1TtULFE5tysRMAarygjX4w9hjTGCqT3SL-PwiMV6aqTtkV3lpqLkc7msVfEdTnyd_pJVFNMM_fjEbeVSuCjiNPVKx7p9sYC1DoWnuKUurt31E1yh2RDwl_oprfKxEF18PP6Q8dXHZe6FeflE3CIxBg","appId":5}]} 

Das Problem ist die JSONArray, wenn Sie die Struktur haben wnat:

{"data":{"phoneId":1,"token":"APA91bF2tN5g1TtULFE5tysRMAarygjX4w9hjTGCqT3SL-PwiMV6aqTtkV3lpqLkc7msVfEdTnyd_pJVFNMM_fjEbeVSuCjiNPVKx7p9sYC1DoWnuKUurt31E1yh2RDwl_oprfKxEF18PP6Q8dXHZe6FeflE3CIxBg","appId":5}} 

einfach löschen jArrayParam und zu tun:

finaljsonobj.put("data", jsonObject); 
0

U kann Wrapper-Klasse für Ihr JSONObject erstellen:

public class YourWrapperClassName{ 
    @SerializedName("phoneId") 
    private int phoneId; 
    @SerializedName("token") 
    private String token; 
    @SerializedName("appId") 
    private int appId; 
} 

Planen Sie Ihre JSON-Objekt und Blick in debug, wie es aussieht:

YourWrapperClassName testObj = new YourWrapperClassName(); 
// initialize testObj fields 

Gson gson = new Gson(); // import com.google.gson.Gson; 
String result = gson.toJson(testObj); // put breakpoint here 

Link zum Download Gson Bibliothek - http://code.google.com/p/google-gson/downloads/list

Verwandte Themen