2013-06-29 95 views
0

的簡便方法,我有以下字符串:安卓:解析這種字符串

{ 
"id":398225253590019, 
"zip":"11375", 
"street":"70-30 Austin St.", 
"state":"NY", 
"longitude":-73.845858172784, 
"latitude":40.720457257566, 
"country":"United States", 
"city":"Forest Hills" 
} 

請任何人都建議我一種方便的方法,用於解析它,這樣我可以做出各種組件的單個對象。

回答

2
import org.json.JSONException; 
import org.json.JSONObject; 

... 
... 

String jsonstring = "{ 
"id":398225253590019, 
"zip":"11375", 
"street":"70-30 Austin St.", 
"state":"NY", 
"longitude":-73.845858172784, 
"latitude":40.720457257566, 
"country":"United States", 
"city":"Forest Hills" 
}"; 

JSONObject jObject = null; 
try{ 
    jObject = new JSONObject(jsonstring); 
catch(JSONException e) { 
    //Json parse error usually 
} 
+0

非常感謝你much1 –

1

的例子:

1.You可以先創建一個對象(說「對象」)的變量ID,郵編,國家等和getter和setter方法。

2.Download Link for jackson.

3.進口圖書館項目。

然後,只需兩行代碼:

ObjectMapper mapper = new ObjectMapper(); 
Object object = mapper.readValue(json, Object.class); 

此對象類將包含值...

Jackson Tutorial.