2013-04-17 135 views
-3

我想在Android代碼中創建此字符串。我想從我的web服務中獲取當前和存儲的緯度日誌。從webservice獲取JSON

{"request": 
    { 
     "from": "$UDID", 
     "with": 
     { 
      "action": "GET_DETAILSTORE", 
      "device": 
      { 
       "id": "$UDID",  
       "time": "1234567890" 
      }, 
      "data": 
      { 
       "cat_id":"1", 
       "more":"10", 
       "currentLat":"23.0333", 
       "currentLong":"72.6167" 
      } 
     } 
    } 
} 
+0

看看JSONObject的助手函數來創建JSON字符串 –

+0

[看起來像JSON。](http://json.org/)所提供的鏈接也有如何解析它的提示。 – Makoto

回答

1

你可以從上面的字符串數據如下:

String strResponse = "YOUR_ABOVE_STRING"; 
JSONTokener tokener = new JSONTokener(strResponse); 
JSONObject finalResult = new JSONObject(tokener); 

JSONObject objRequest = finalResult.getJSONObject("request"); 

Log.i("from",objRequest.getJSONString("from")); 

JSONObject objWith = objRequest.getJSONObject("with"); 

Log.i("action",objWith.getJSONString("action")); 

JSONObject objDevice = objWith.getJSONObject("device"); 

Log.i("id",objDevice.getJSONString("id")); 
Log.i("time",objDevice.getJSONString("time")); 

JSONObject objData = objWith.getJSONObject("data"); 

Log.i("cat_id",objData.getJSONString("cat_id")); 
Log.i("more",objData.getJSONString("more")); 
Log.i("currentLat",objData.getJSONString("currentLat")); 
Log.i("currentLong",objData.getJSONString("currentLong")); 

現在你可以使用此類數據來自在JSON格式來從Web服務的字符串。

希望它能幫助你。