2013-01-23 51 views
3

我想檢索此json格式文檔中所有元素的值。循環瀏覽不帶子名稱的json對象

[ 
      { 
       "id": "c0001xgp", 
       "title": 8.8, 
       "content": 142.369, 
       "vignette": "us", 
       "image": "2011-03-11 04:46:23", 
       "type": 24.4, 
       "pubdate": 38.322 
      }, 
      { 
       "id": "c0001xgp", 
       "title": 8.8, 
       "content": 142.369, 
       "vignette": "us", 
       "image": "2011-03-11 04:46:23", 
       "type": 24.4, 
       "pubdate": 38.322 
      }, { 
       "id": "c0001xgp", 
       "title": 8.8, 
       "content": 142.369, 
       "vignette": "us", 
       "image": "2011-03-11 04:46:23", 
       "type": 24.4, 
       "pubdate": 38.322 
      }, { 
       "id": "c0001xgp", 
       "title": 8.8, 
       "content": 142.369, 
       "vignette": "us", 
       "image": "2011-03-11 04:46:23", 
       "type": 24.4, 
       "pubdate": 38.322 
      } 

]

我怎樣才能做到這一點。我正在學習json和android。當json對象中有一個父對象時,我可以處理,但現在我很難解決這個問題。

主類

// JSON Node names 
private static final String TAG_ARRAY= ""; 

private static final String TAG_ID = "id"; 

private static final String TAG_TITLE = "title"; 

private static final String TAG_CONTENT = "content"; 

private static final String TAG_VIGNETTE = "vignette"; 

private static final String TAG_IMAGE = "image"; 

private static final String TAG_TYPE = "type"; 

private static final String TAG_PUBDATE = "pubdate"; 


// contacts JSONArray 

JSONArray myArray = null; 

@Override 

public void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 

    setContentView(R.layout.actus); 

    // Hashmap for ListView 
    ArrayList<HashMap<String, String>> contactList = new ArrayList<HashMap<String, String>>(); 

    // Creating JSON Parser instance 

    JSONParser jParser = new JSONParser(); 

    // getting JSON string from URL 
    JSONArray jArray = jParser.getJSONFromUrl(url); 


     // looping through All element 
     for(int i = 0; i < jArray.length(); i++){ 


      JSONObject oneObject = null; 
      try{ 

      oneObject = jArray.getJSONObject(i); 

      // Storing each json item in variable 
      String id; 

      String title; 

       title = oneObject.getString(TAG_TITLE); 


      String content = oneObject.getString(TAG_CONTENT); 

      String vignette = oneObject.getString(TAG_VIGNETTE); 

      String image = oneObject.getString(TAG_IMAGE); 

      String type = oneObject.getString(TAG_TYPE); 

      String pubdate = oneObject.getString(TAG_PUBDATE); 


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

      // adding each child node to HashMap key => value 

      map.put(TAG_TITLE, title); 

      map.put(TAG_CONTENT, content); 

      map.put(TAG_VIGNETTE, vignette); 

      map.put(TAG_TYPE, type); 

      map.put(TAG_PUBDATE, pubdate); 



      // adding HashList to ArrayList 

      contactList.add(map); 


      }catch (JSONException e) { 

       // TODO Auto-generated catch 

塊 e.printStackTrace(); }

 } 



    /** 
    * Updating parsed JSON data into ListView 
    */ 
    ListAdapter adapter = new SimpleAdapter(this, contactList, 
      R.layout.list_item, 
      new String[] { TAG_TITLE, TAG_CONTENT, TAG_VIGNETTE }, new int[] { 
        R.id.name, R.id.email, R.id.mobile }); 

    setListAdapter(adapter); 

    // selecting single ListView item 
    ListView lv = getListView(); 

    // Launching new screen on Selecting Single ListItem 

    lv.setOnItemClickListener(new OnItemClickListener() { 

     @Override 
     public void onItemClick(AdapterView<?> parent, View view, 
       int position, long id) { 
      // getting values from selected ListItem 
      String name = ((TextView) view.findViewById(R.id.name)).getText().toString(); 

      String cost = ((TextView) view.findViewById(R.id.email)).getText().toString(); 

      String description = ((TextView) view.findViewById(R.id.mobile)).getText().toString(); 

      // Starting new intent 
      Intent in = new Intent(getApplicationContext(), SingleMenuItemActivity.class); 
      in.putExtra(TAG_VIGNETTE, name); 

      in.putExtra(TAG_CONTENT, cost); 

      in.putExtra(TAG_VIGNETTE, description); 

      startActivity(in); 

     } 
    }); 


} 
+0

你可以發佈整個JSON你得到什麼?有什麼缺失? –

回答

2

解析當前JSON字符串爲從JSON對象獲得的所有值:

JSONArray jArray = new JSONArray("your json String"); 

for (int i=0; i < jArray.length(); i++) 
{ 
    JSONObject oneObject = jArray.getJSONObject(i); 
    // get all value here 
    String str_eqid=oneObject.getString("eqid"); 
    String str_magnitude=oneObject.getString("magnitude"); 
    String str_lng=oneObject.getString("lng"); 
    String str_src=oneObject.getString("src"); 

    // get other values from jsonobject in same way 
} 

,並使用ArrayListHashMap用於存儲值

+0

請問這是什麼「你的json字符串」?是它的網址 – Dimitri

+0

@sophia:「你的json字符串」意味着你從服務器 –

+0

得到什麼結果json以「[{」僅在服務器上。這就是我的麻煩 – Dimitri

0
JSONArray jsonArray = new JSONArray(YourString); 
for(int i = 0; i < jsonArray.length(); i++) { 
    JsonObject jsonObject = jsonArray.getJSONObject(i); 
    System.out.println(jsonObject.getString("eqid")); 
} 
3

「正如你所說的有沒有父母,所以你必須使用JSONArray,通過它你需要循環並獲取所有的JSON對象。「

try 
    { JSONArray mp3 = new JSONArray(jsonStr); 
    JSONObject c = new JSONObject();       

        // looping through All 
        for (int i = 0; i < mp3.length(); i++) 
        { 
         try 
         { 
          c = mp3.getJSONObject(i); 

          String id = c.getString(TAG_mp3_id); 
          String title = c.getString(TAG_title); 


          // tmp hashmap for single contact 
       HashMap<String, String> contact = new HashMap<String,String>(); 

          // adding each child node to HashMap key => value 
          contact.put(TAG_mp3_id, id); 
          contact.put(TAG_title, title); 


          // adding contact to contact list 
          mp3List.add(contact); 


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

       } 

「希望這會幫助你。」