String jsonStr = '{"menu": {' +
'"id": "file",' +
'"value": "File",' +
'"popup": {' +
'"menuitem": [' +
'{"value": "New", "onclick": "CreateNewDoc()"},' +
'{"value": "Open", "onclick": "OpenDoc()"},' +
'{"value": "Close", "onclick": "CloseDoc()"}' +
']' +
'}' +
'}}';
JSON स्ट्रिंग http://json.org/example.html से वास्तव में है यही कारण है कि। यह सबसे अच्छा था जो मुझे इस उदाहरण के लिए मिल सकता था।
अब हमारे पास यह जगह है, JSONObject
का उपयोग करना शुरू करें। आप नीचे दिए गए आयात की जरूरत है इस के लिए काम करने के लिए होगा: import org.json.JSONObject;
JSONObject jsonObj = new JSONObject(jsonStr);
कि instantiated साथ
, हम JSON स्ट्रिंग से डेटा के विभिन्न टुकड़े को नहीं निकाला जा निम्न कर सकते हैं -
// grabbing the menu object
JSONObject menu = jsonObj.getJSONObject("menu");
Reading =========> HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
is.close();
result=sb.toString();=======>Here result is the json string
// these 2 are strings
String id = menu.getString("id");
String value = menu.getString("value");
// the popop is another JSON object
JSONObject popup = menu.getJSONObject("popup");
// using JSONArray to grab the menuitems from under popop
JSONArray menuitemArr = popupObject.getJSONArray("menuitem");
// lets loop through the JSONArray and get all the items
for (int i = 0; i < menuitemArr.length(); i++) {
// printing the values to the logcat
Log.v(menuitemArr.getJSONObject(i).getString("value").toString());
Log.v(menuitemArr.getJSONObject(i).getString("onclick").toString());
}
एक आसान उदाहरण के लिए here
स्रोत
2012-07-20 12:52:48
उपयोग httpclient एंड्रॉयड पर पर JSON प्रारूप में अपने तार के साथ डेटा विनिमय करने के सर्वर। अगर आप अटक जाते हैं, तो अपना कोड पोस्ट करें और हम मदद करने की कोशिश करेंगे। –