2014-07-23 114 views
1
  • 我試圖從json獲取數據。我可以在第一狀態獲得數據。
  • 但如何獲取數據「升序」和「降序」,並顯示在另一個在列表視圖中的活動?

,這裏是我的JsonAndroid從json獲取數據並排序

[{"category_name":"Food","filter_type":"Sort by","field_name":"","type":"VALUE","table_name":"","item_list":["Ascending","Descending"]} 

這是我的Java代碼

if (jsonStr != null) { 
    try { 

     foods = new JSONArray(jsonStr); 


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

      if(c.getString("category_name").equals("Food")) { 

       String category_name = c.getString(TAG_CATEGORY_NAME); 
       String table_name = c.getString(TAG_TABLE_NAME); 
       String item_list = c.getString(TAG_ITEM_LIST); 

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

       // adding each child node to HashMap key => value 
       contact.put(TAG_CATEGORY_NAME, category_name); 
       contact.put(TAG_TABLE_NAME, table_name); 
       contact.put(TAG_ITEM_LIST, item_list); 

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

      } 
     } 
    } catch (JSONException e) { 
     e.printStackTrace(); 
    } 
} else { 
    Log.e("ServiceHandler", "Couldn't get any data from the url"); 
} 

我試圖按照本教程http://www.androidhive.info/2012/01/android-json-parsing-tutorial/,但我還是不完全理解。

回答

1

讓我來解釋這一點。

[意思是它的一個數組。 {是一個對象。

在你的情況下,它是一個數組,其中包含名爲category_name,filter_type和field_name的-JSONObject。 type和table_name以及一個新的帶有對象item_list的jsonarray。

如何解析這個字符串?

下面是一個例子:

String str = "[{"category_name":"Food","filter_type":"Sort by","field_name":"","type":"VALUE","table_name":"","item_list":["Ascending","Descending"]}"; 

JSONArray jsonArray = new JSONArray(str); 
//now it holds the JSONObject. 
for (int i = 0; i<= jsonArray.length(); i++) { 
//now we loop through and get the jsonObject 
JSONObject jsonObj = new JSONObject(jsonArray.getJsonObject(i)); 
//now it contains your data. 
Log.d("Category_nameValue=", jsonObj.getString("category_name")); 
//now we want to get the array from the item_list. 
JSONArray itemList = new JSONArray(jsonObj.getString("item_list")); 
//now itemList.getString(1); === Ascending while itemList.getString(2) == Descending 



//now itemList contains several new objects which can also be looped as the parent one. 
} 

既然你已經知道如何創建一個JSONArray,你就可以開始整理吧。

這已經被回答了在Android how to sort JSONArray of JSONObjects

如果您希望將這些數據發送到另一個活動,你可以使用JSONArray.toString()方法,並通過意圖發送。

這是在Pass a String from one Activity to another Activity in Android

希望這有助於解釋方便。

+0

感謝您的解釋。但我的意思是不對數據進行排序。如果您看到item_list,則陣列中有兩個數據(升序和降序)。 我想要的是獲取這些數據,並將其顯示在另一個列表視圖中。 – Matthew

+0

已更新,查看意見 –

+0

非常感謝:D。現在我要做的就是從一個意圖中列出一個清單,對。 – Matthew

1

要從JSONObject c 乾脆寫拿到JSONArray

JSONArray itemList = c.getJSONArray(name); 

那麼你可以通過數據迭代像任何其他數組

for (int i = 0; i < itemList.length(); i++) { 
    // do something 
} 
+0

你能否給你的答案添加一些解釋?代碼只有答案(有時)是好的,但代碼+解釋答案是(最多次)更好 – Barranka

+0

對不起,關於.. – erik

1

如果你是新手,我會建議你考慮使用Gson來直接解析你的Json響應到一個Java實體類。所以你會避免手動分析你的所有答案。

你的JSON響應

[{"category_name":"Food","filter_type":"Sort by","field_name":"","type":"VALUE","table_name":"","item_list":["Ascending","Descending"]} 

表示響應

public class MyEntity { 
    String category_name; 
    String filter_type; 
    String field_name; 
    String type; 
    String table_name; 
    String [] item_list; 

    // getters/setters ... 
} 

解析響應

Gson gson = new Gson(); 
MyEntity myEntity = gson.fromJson(response, MyEntity.class); 
實體

最後,發送數據,先從羣衆演員

Intent intent = new Intent(this, AnotherActivity.class); 
intent.putExtra("EXTRA_DATA", myEntity.getCategoryName()); 
startActivity(intent); 

現在你恢復您AnotherActivity

Intent intent = getIntent(); 
String categoryName = intent.getStringExtra("EXTRA_DATA"); 

額外數據,並且您可以使用填充的ListView新活動一個ArrayAdapter:Example

+0

感謝您的建議:D。 我會試試看 – Matthew