2012-12-18 52 views
0

我有一个应用程序,它与web服务连接。我发送Json的一些数据:看看看起来如何json

  try { 


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

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

     } catch (Exception e) { 
      // TODO: handle exception 
     } 

我怎样才能看到如何看起来这个json? 我想服务器看到这样的内容:

{"Token": [ 
{ 

"token": "asdasfasf", 

"appId": 8.8, 

"phoneId": 142.369, 

} 
+0

看到JSON在哪里?在你的Android客户端,或在服务器上? –

+0

你的例子json它无效 –

+0

我不明白的问题 – njzk2

回答

1

创建JSON对象为:

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("Token", jArrayParam); 

现在finaljsonobj JSON对象看起来像是:

{ 
    "Token": [ 
    { 
     "token": "asdasfasf", 
     "appId": 8.8, 
     "phoneId": 142.369, 

    } 
    ] 
} 
0

使用JSON.stringify(json_object)

0

我们假设你的JSON实际上是有效的(它不是),并且你有一个功能完善的JSONObject。如果你想在控制台中看到它,你只需要这样做:

System.out.println(myAwesomeJSONObject.toString(2)); 

1表示缩进空格的数量。我喜欢2个缩进空间,但这是个人品味和可读性的问题。