2013-07-12 51 views
1
[ 
    { 
     "name":   "The Universe & The Earth" 
     , "imagename":  "cat1.jpg" 
     , "active":   "Y" 
     , "createdon":  "1901-01-01" 
     , "lastmodifiedon": "1901-01-01 00:00:00" 
     , "description": "Knowledge of Earth location in the universe has been shaped by 400 years of telescopic observations, and has expanded radically in the last century.\n" 
     , "id":    "1" 
    } 
    , { 
     "name":   "Life on Earth" 
     , "imagename":  "cat2.jpg" 
     , "active":   "Y" 
     , "createdon":  "1901-01-01" 
     , "lastmodifiedon": "1901-01-01 00:00:00" 
     , "description": "Over the last 3.7 billion years or so, living organisms on the Earth have diversified and adapted to almost every environment imaginable." 
     , "id":    "2" 
    } 
] 

這是我的json值。現在我想解析並顯示在自定義列表視圖中,我如何可以 這樣做?我跟着http://www.androidhive.info/2012/01/android-json-parsing-tutorial/這是鏈接但不能實現。我怎樣才能做到這一點?有人可以告訴我嗎?提前致謝。如何在android中解析沒有json對象標題的json數組?

+1

'JSONArray array = new JSONArray(theString);' – Blackbelt

+0

是這個jsonArray,裏面有json對象嗎? –

+0

是的。JsonArray裏面保存着JsonObjects。 –

回答

6

這是一個JSONArray,而不是一個JSONObject - 通過它進行一個JSONObject,使用

JSONObject jsonObject = jsonArray.getJSONObject(0); 

這個從這個JSONArray獲得第一的JSONObject。

如果您有多個一個JSONObjects,使用此:

JSONObject jsonObject; 
for(int n = 0; n < jsonArray.length(); n++) 
{ 
    jsonObject = jsonArray.getJSONObject(n); 
} 

爲了獲取值:

jsonObject.getString("name"); 
3

嘗試下面的代碼。

JSONArray jArr = new JSONArray(your_json_string); 

for (int count = 0; count < jArr.length(); count++) { 
    JSONObject obj = jArr.getJSONObject(count); 
    String name = obj.getString("name"); 
    String imageName = obj.getString("imagename"); 
    //so on 
} 
+0

嗨。什麼是your_json_string在這裏? –

+1

@ShivamBhalla,「your_json_string」將是寫入問題的字符串。我根據問題給出了答案,並使用相同的問題JSON字符串。 :) –

0

您可以簡單地使用地圖通過所有鍵和的JSONObject的值進行迭代,

 String jsonString = "[{\"name\":\"The Universe & The Earth\", \"imagename\":\"cat1.jpg\"}, {\"name\":\"Life on Earth\", \"imagename\":\"cat2.jpg\"}]"; 
     JSONArray array; 
     try { 
      array = new JSONArray(jsonString); 
      JSONObject object; 
      Map<String,String> map; 
      for (int i = 0; i < array.length(); i++) { 
       object = new JSONObject(array.getJSONObject(i).toString()); 
       map = new HashMap<String,String>(); 
       Iterator<?> iter = object.keys(); 
       while(iter.hasNext()){ 
        String key = (String)iter.next(); 
        String value = object.getString(key); 
        map.put(key,value); 
       } 
       System.out.println(map.toString()); 
      } 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 
1

首先,我創建解析器類JSONParser.java

package com.example.myparse; 

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.io.UnsupportedEncodingException; 
import org.apache.http.HttpEntity; 
import org.apache.http.HttpResponse; 
import org.apache.http.StatusLine; 
import org.apache.http.client.ClientProtocolException; 
import org.apache.http.client.HttpClient; 
import org.apache.http.client.methods.HttpGet; 
import org.apache.http.client.methods.HttpPost; 
import org.apache.http.impl.client.DefaultHttpClient; 
import org.json.JSONArray; 
import org.json.JSONException; 
import org.json.JSONObject; 
import android.util.Log; 

public class JSONParser { 

static InputStream is = null; 
static JSONArray jarray = null; 
static String json = ""; 

// constructor 
public JSONParser() { 

} 

public JSONArray getJSONFromUrl(String url) { 

     StringBuilder builder = new StringBuilder(); 
     HttpClient client = new DefaultHttpClient(); 
     HttpGet httpGet = new HttpGet(url); 
     try { 
      HttpResponse response = client.execute(httpGet); 
      StatusLine statusLine = response.getStatusLine(); 
      int statusCode = statusLine.getStatusCode(); 
      if (statusCode == 200) { 
      HttpEntity entity = response.getEntity(); 
      InputStream content = entity.getContent(); 
      BufferedReader reader = new BufferedReader(new InputStreamReader(content)); 
      String line; 
      while ((line = reader.readLine()) != null) { 
       builder.append(line); 
      } 
      } else { 
      Log.e("==>", "Failed to download file"); 
      } 
     } catch (ClientProtocolException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

    // try parse the string to a JSON object 
    try { 
     jarray = new JSONArray(builder.toString()); 
     //System.out.println(""+jarray); 
    } catch (JSONException e) { 
     Log.e("JSON Parser", "Error parsing data " + e.toString()); 
    } 

    // return JSON String 
    return jarray; 

} 
} 

然後創建在這樣主類的對象....

ArrayList<HashMap<String, String>> contactList = new ArrayList<HashMap<String, String>>(); // used to display in list view 

JSONParser jParser = new JSONParser(); 
JSONArray json = jParser.getJSONFromUrl(url); // pass your ulr here 

for(int i = 0; i <= json.length(); i++) // using for loop for parsing 
{ 
    try 
    { 
    JSONObject c = json.getJSONObject(i); 
    String name = c.getString(TAG_NAME); 
    String imagename = c.getString(TAG_IMAGENAME); 
    String active = c.getString(TAG_ACTIVE); 
    String createdon = c.getString(TAG_CREATEDON); 
    String lastmodifiedon = c.getString(TAG_LASTMODIFIEDON); 
    String description = c.getString(TAG_DESCRIPTION); 
    String id = c.getString(TAG_ID); 

      // If you want to show your parsed value in list view add the values into the array list    

    // creating new HashMap 
    HashMap<String, String> map = new HashMap<String, String>(); 

      // adding each child node to HashMap key => value 
      map.put(TAG_NAME, name); 
      map.put(TAG_IMAGENAME, imagename); 
    map.put(TAG_ACTIVE, active); 
    map.put(TAG_CREATEDON, createdon); 
    map.put(TAG_LASTMODIFIEDON, lastmodifiedon); 
    map.put(TAG_DESCRIPTION, description); 
    map.put(TAG_ID, id); 
     // adding HashList to ArrayList 
    contactList.add(map); 
System.out.println("contactlist---->"+contactList); 
} 

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

我這樣做了。我得到了正確的輸出。祝一切順利。