2012-05-23 13 views
13

मुझे लगता है कि मैं प्रारूपएंड्रॉइड में JSON प्रारूप डेटा कैसे बनाएं?

{ 
    "k2": { 
    "mk1": "mv1", 
    "mk2": [ 
     "lv1", 
     "lv2" 
    ] 
    } 
} 

तो कैसे एंड्रॉयड में इस प्रारूप उत्पन्न कर सकते हैं नीचे के रूप में पारित करने के लिए की जरूरत है सर्वर से कुछ मानकों को पारित करने की जरूरत है।

मैंने As shown in example 5.3 का उपयोग करके यह कोशिश की लेकिन यह obj.writeJSONString(out); पर इस त्रुटि को दिखा रहा है। क्या कोई इसे हल करने में मदद कर सकता है।

धन्यवाद अग्रिम

उत्तर

37

इसके कि हालांकि सभी में, उत्पादन आप चाहते हैं JSONObject और JSONObject एक और JSONObject अंदर अंदर JSONArray है नहीं है। तो, आप उन्हें अलग-अलग बना सकते हैं और फिर एक साथ रख सकते हैं। नीचे के अनुसार।

try { 
      JSONObject parent = new JSONObject(); 
      JSONObject jsonObject = new JSONObject(); 
      JSONArray jsonArray = new JSONArray(); 
      jsonArray.put("lv1"); 
      jsonArray.put("lv2"); 

      jsonObject.put("mk1", "mv1"); 
      jsonObject.put("mk2", jsonArray); 
      parent.put("k2", jsonObject); 
      Log.d("output", parent.toString(2)); 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 

आउटपुट

 { 
      "k2": { 
      "mk1": "mv1", 
      "mk2": [ 
       "lv1", 
       "lv2" 
      ] 
      } 
     } 
+0

धन्यवाद यह ठीक काम कर रहा है – Harish

+0

+1 मैं JSON पार्सिंग में नया हूं। यह स्पष्ट उदाहरण मुझे जरूरी था :-) – rockstar

+0

यह अद्भुत सुझाव है ... – DJhon

6

में आप JSONObject उपयोग करें और यह के साथ अपने डेटा का निर्माण कर सकते हैं।

यहाँ Documentation link

jsonObject.toString() // Produces json formatted object 
+0

यह भी काम नहीं कर रहा है। – Harish

+0

क्या आप अपवाद विवरण पोस्ट कर सकते हैं? – ChristopheCVB

+0

क्या आप 'org.json.JSONObject' आयात कर रहे हैं? – ChristopheCVB

2

हाय पहले आप है कोड

public class HttpUtil { 

// lat=50.2911 lon=8.9842 

private final static String TAG = "DealApplication:HttpUtil"; 

public static String get(String url) throws ClientProtocolException, 
     IOException { 
    Log.d(TAG, "HTTP POST " + url); 
    HttpGet post = new HttpGet(url); 
    HttpResponse response = executeMethod(post); 
    return getResponseAsString(response); 
} 

public static String post(String url, HashMap<String, String> httpParameters) 
     throws ClientProtocolException, IOException { 
    Log.d(TAG, "HTTP POST " + url); 
    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(
      httpParameters.size()); 
    Set<String> httpParameterKeys = httpParameters.keySet(); 
    for (String httpParameterKey : httpParameterKeys) { 
     nameValuePairs.add(new BasicNameValuePair(httpParameterKey, 
       httpParameters.get(httpParameterKey))); 
    } 

    HttpPost method = new HttpPost(url); 
    UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(nameValuePairs); 
    System.out.println("**************Request=>"+urlEncodedFormEntity.toString()); 
    method.setEntity(urlEncodedFormEntity); 
    HttpResponse response = executeMethod(method); 

    return getResponseAsString(response); 
} 

private static HttpResponse executeMethod(HttpRequestBase method) 
     throws ClientProtocolException, IOException { 
    HttpResponse response = null; 
    HttpClient client = new DefaultHttpClient(); 
    response = client.execute(method); 
    Log.d(TAG, "executeMethod=" + response.getStatusLine()); 
    return response; 
} 

private static String getResponseAsString(HttpResponse response) 
     throws IllegalStateException, IOException { 
    String content = null; 
    InputStream stream = null; 
    try { 
     if (response != null) { 
      stream = response.getEntity().getContent(); 
      InputStreamReader reader = new InputStreamReader(stream); 
      BufferedReader buffer = new BufferedReader(reader); 
      StringBuilder sb = new StringBuilder(); 
      String cur; 
      while ((cur = buffer.readLine()) != null) { 
       sb.append(cur + "\n"); 
      } 
      content = sb.toString(); 
      System.out.println("**************Response =>"+content); 
     } 
    } finally { 
     if (stream != null) { 
      stream.close(); 
     } 
    } 
    return content; 
} 

} 
0

यह उदाहरण निम्नलिखित अलग वर्ग HttpUtil.java.See बनाने के लिए मदद से आप सिर्फ इस फ़ंक्शन को कॉल करें यह JSON को स्ट्रिंग मान के रूप में वापस कर देगा .. इसे आजमाएं

public String getResult() { 
    JSONObject userResults = null; 
    try { 
     userResults = new JSONObject(); 
     userResults.put("valueOne",str_one); 
     userResults.put("valueTwo", str_two); 
     userResults.put("valueThree" ,str_three); 
     userResults.put("valueFour", str_four); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    return userResults.toString(); 
} 
संबंधित मुद्दे