2012-03-07 18 views
51

मैं कैसे एंड्रॉयड में एक JSON फ़ीड पार्स करने के लिए पर कदम दर कदम निर्देश कहां मिल सकती है में JSON पार्स करने के लिए? मैं सिर्फ एक Android जानने के लिए इच्छुक शुरुआत कर रहा हूँ।कैसे एंड्रॉयड

+2

एक json पार्सर एसडीके में एम्बेडेड है के साथ शुरू करने के लिए मदद मिलेगी। http://developer.android.com/reference/org/json/package-summary.html – njzk2

+0

http://stackoverflow.com/a/2840873/643350 – Dipin

+0

पर सभी ** संबंधित ** लिंक देखें दाहिने तरफ - समान प्रश्नों का _ton_ है। प्रश्न पूछने से पहले हम कुछ प्रयासों की सराहना करते हैं। –

उत्तर

3

मैंने आपके लिए एक सरल उदाहरण तैयार किया है और स्रोत को एनोटेट किया है। उदाहरण दिखाता है कि लाइव json हड़पने और विस्तार निकासी के लिए एक JSONObject में पार्स करने के लिए:

try{ 
    // Create a new HTTP Client 
    DefaultHttpClient defaultClient = new DefaultHttpClient(); 
    // Setup the get request 
    HttpGet httpGetRequest = new HttpGet("http://example.json"); 

    // Execute the request in the client 
    HttpResponse httpResponse = defaultClient.execute(httpGetRequest); 
    // Grab the response 
    BufferedReader reader = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent(), "UTF-8")); 
    String json = reader.readLine(); 

    // Instantiate a JSON object from the request response 
    JSONObject jsonObject = new JSONObject(json); 

} catch(Exception e){ 
    // In your production code handle any errors and catch the individual exceptions 
    e.printStackTrace(); 
} 

बार जब आप अपने JSONObject डेटा की आवश्यकता को निकालने के लिए कैसे पर जानकारी के लिए SDK का उल्लेख किया है।

+0

हाय मैंने इसे अंदर रखा है लेकिन त्रुटियां प्राप्त कर रही हैं मैंने सबकुछ आयात किया है लेकिन अभी भी समस्याएं हैं – iamlukeyb

+0

आपको कोशिश करने के लिए उपरोक्त कोड ब्लॉक को लपेटने की आवश्यकता होगी, मैंने इसे प्रतिबिंबित करने के लिए कोड संपादित किया है। – Ljdawson

+0

अभी भी समस्याएं हैं? – Ljdawson

111

एंड्रॉइड में आपके पास जेसन निर्मित अंतर्निहित सभी टूल्स हैं। उदाहरण इस प्रकार है, GSON या ऐसा कुछ के लिए कोई जरूरत नहीं।

आपके JSON प्राप्त करें:

DefaultHttpClient httpclient = new DefaultHttpClient(new BasicHttpParams()); 
HttpPost httppost = new HttpPost(http://someJSONUrl/jsonWebService); 
// Depends on your web service 
httppost.setHeader("Content-type", "application/json"); 

InputStream inputStream = null; 
String result = null; 
try { 
    HttpResponse response = httpclient.execute(httppost);   
    HttpEntity entity = response.getEntity(); 

    inputStream = entity.getContent(); 
    // json is UTF-8 by default 
    BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8); 
    StringBuilder sb = new StringBuilder(); 

    String line = null; 
    while ((line = reader.readLine()) != null) 
    { 
     sb.append(line + "\n"); 
    } 
    result = sb.toString(); 
} catch (Exception e) { 
    // Oops 
} 
finally { 
    try{if(inputStream != null)inputStream.close();}catch(Exception squish){} 
} 
अब आप तो अपने JSON है,

क्या?

एक JSONObject बनाएँ:

JSONObject jObject = new JSONObject(result); 

प्राप्त करने के लिए एक विशिष्ट स्ट्रिंग

String aJsonString = jObject.getString("STRINGNAME"); 

प्राप्त करने के लिए एक विशिष्ट बूलियन

boolean aJsonBoolean = jObject.getBoolean("BOOLEANNAME"); 

एक विशिष्ट पूर्णांक

int aJsonInteger = jObject.getInt("INTEGERNAME"); 

प्राप्त करने के लिए प्राप्त करने के लिए एक विशिष्ट लंबे

long aJsonLong = jObject.getBoolean("LONGNAME"); 

प्राप्त करने के लिए एक विशिष्ट डबल

double aJsonDouble = jObject.getDouble("DOUBLENAME"); 

एक विशिष्टप्राप्त करने के लिए:

JSONArray jArray = jObject.getJSONArray("ARRAYNAME"); 

सरणी

for (int i=0; i < jArray.length(); i++) 
{ 
    try { 
     JSONObject oneObject = jArray.getJSONObject(i); 
     // Pulling items from the array 
     String oneObjectsItem = oneObject.getString("STRINGNAMEinTHEarray"); 
     String oneObjectsItem2 = oneObject.getString("anotherSTRINGNAMEINtheARRAY"); 
    } catch (JSONException e) { 
     // Oops 
    } 
} 
+0

जब आप JSONArray प्राप्त करते हैं तो भी एक मामला हो सकता है और यदि आप JSONObject jObject = new JSONObject (परिणाम) की कोशिश करते हैं - तो आपको पार्सिंग के बारे में एक निष्पादन मिलेगा। ऐसे मामले में JSONArray jArray = नया JSONArray (परिणाम) काम करेगा। – Stan

9
  1. लेखन JSON पार्सर कक्षा

    public class JSONParser { 
    
        static InputStream is = null; 
        static JSONObject jObj = null; 
        static String json = ""; 
    
        // constructor 
        public JSONParser() {} 
    
        public JSONObject getJSONFromUrl(String url) { 
    
         // Making HTTP request 
         try { 
          // defaultHttpClient 
          DefaultHttpClient httpClient = new DefaultHttpClient(); 
          HttpPost httpPost = new HttpPost(url); 
    
          HttpResponse httpResponse = httpClient.execute(httpPost); 
          HttpEntity httpEntity = httpResponse.getEntity(); 
          is = httpEntity.getContent(); 
    
         } catch (UnsupportedEncodingException e) { 
          e.printStackTrace(); 
         } catch (ClientProtocolException e) { 
          e.printStackTrace(); 
         } catch (IOException e) { 
          e.printStackTrace(); 
         } 
    
         try { 
          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(); 
          json = sb.toString(); 
         } catch (Exception e) { 
          Log.e("Buffer Error", "Error converting result " + e.toString()); 
         } 
    
         // try parse the string to a JSON object 
         try { 
          jObj = new JSONObject(json); 
         } catch (JSONException e) { 
          Log.e("JSON Parser", "Error parsing data " + e.toString()); 
         } 
    
         // return JSON String 
         return jObj; 
    
        } 
    } 
    
  2. पार्सिंग JSON डेटा एक बार जब आप पार्सर श्रेणी का निर्माण से आइटम प्राप्त करने के लिए अगले थी एनजी कैसे उस वर्ग का उपयोग करने के लिए है। नीचे मैं कैसे json (इस उदाहरण में लिया गया) पार्सर वर्ग का उपयोग कर पार्स करने के लिए समझा रहा हूँ।

2.1।इन सभी नोड नामों को चर में स्टोर करें: संपर्कों में जेसन हमारे पास नाम, ईमेल, पता, लिंग और फोन नंबर जैसे आइटम हैं। तो सबसे पहले इन सभी नोड नामों को चर में स्टोर करना है। अपनी मुख्य गतिविधि कक्षा खोलें और स्थैतिक चर में सभी नोड नामों को स्टोर करें।

// url to make request 
private static String url = "http://api.9android.net/contacts"; 

// JSON Node names 
private static final String TAG_CONTACTS = "contacts"; 
private static final String TAG_ID = "id"; 
private static final String TAG_NAME = "name"; 
private static final String TAG_EMAIL = "email"; 
private static final String TAG_ADDRESS = "address"; 
private static final String TAG_GENDER = "gender"; 
private static final String TAG_PHONE = "phone"; 
private static final String TAG_PHONE_MOBILE = "mobile"; 
private static final String TAG_PHONE_HOME = "home"; 
private static final String TAG_PHONE_OFFICE = "office"; 

// contacts JSONArray 
JSONArray contacts = null; 

2.2। JSONObject प्राप्त करने के लिए पार्सर क्लास का उपयोग करें और प्रत्येक जेसन आइटम के माध्यम से लूपिंग करें। नीचे मैं JSONParser क्लास का एक उदाहरण बना रहा हूं और लूप के लिए उपयोग कर रहा हूं, मैं प्रत्येक जेसन आइटम के माध्यम से लूपिंग कर रहा हूं और अंततः प्रत्येक जेसन डेटा को चर में संग्रहीत कर रहा हूं।

// Creating JSON Parser instance 
JSONParser jParser = new JSONParser(); 

// getting JSON string from URL 
JSONObject json = jParser.getJSONFromUrl(url); 

try { 
    // Getting Array of Contacts 
    contacts = json.getJSONArray(TAG_CONTACTS); 

    // looping through All Contacts 
    for(int i = 0; i < contacts.length(); i++){ 
     JSONObject c = contacts.getJSONObject(i); 

     // Storing each json item in variable 
     String id = c.getString(TAG_ID); 
     String name = c.getString(TAG_NAME); 
     String email = c.getString(TAG_EMAIL); 
     String address = c.getString(TAG_ADDRESS); 
     String gender = c.getString(TAG_GENDER); 

     // Phone number is agin JSON Object 
     JSONObject phone = c.getJSONObject(TAG_PHONE); 
     String mobile = phone.getString(TAG_PHONE_MOBILE); 
     String home = phone.getString(TAG_PHONE_HOME); 
     String office = phone.getString(TAG_PHONE_OFFICE); 

    } 
} catch (JSONException e) { 
    e.printStackTrace(); 
} 
0

प्रयास करें निम्नलिखित इस ट्यूटोरियल http://www.androidhive.info/2012/01/android-json-parsing-tutorial/ आशा यह आप JSONParsing

+0

मैंने एंड्रॉइड हाइव (http://www.androidhive.info/2012/01/android-json-parsing-tutorial/) से नमूना करने की कोशिश की ....... लेकिन साइट के लेखक में कुछ टाइपो त्रुटियां हैं यह ... लेकिन शुरुआती लोगों के लिए एंड्रॉइड प्रोग्रामिंग सीखने के लिए यह एक महान जगह है ... स्रोत के लिए धन्यवाद! – Devrath