我想从下面嵌套的json字符串中获取值并将其传递给listview,我对此感到困惑。有人可以帮助解决这个问题。谢谢提前
JSON DATA:
{
"errstr": "Apps list",
"success": 1,
"data": {
"topFree": [
{
"AppID": "3",
"Title": "rgrger",
"Price": null,
"Rating": ""
},
{
"AppID": "10",
"Title": "dwdqw",
"Price": null,
"Rating": ""
}
],
"topPaid": [
{
"AppID": "14",
"Title": "erfwerwe",
"Price": "0.00",
"Rating": ""
}
],
"magazine": [
{
"AppID": "65",
"Title": "wdfwfqw",
"Price": "0.00",
"Rating": ""
}
],
"books": "",
"onlineApp": ""
}
}
答案 0 :(得分:1)
试试这个..
try {
JSONObject result = new JSONObject(response);
JSONObject data = result.getJSONObject("data");
// check for your conditions... i am directly calling array now..
if(data.has("topFree"){
JSONArray array = data.getJSONArray("topFree");
//creating array for storing values... you caN create with your class..
arr_list = new ArrayList<String>();
for (int i = 0; i < array.length(); i++) {
JSONObject obj = array.getJSONObject(i);
String AppID = ""+obj.getString("AppID");
String Title = ""+obj.getString("Title");
String Price = ""+obj.getString("Price");
String Rating = ""+obj.getString("Rating");
arr_list.add(AppID); // adding values to array.
}
}
// do the same for other arrays
} catch (JSONException e) {
e.printStackTrace();
}
获取数据后..您可以向listview适配器添加值..
答案 1 :(得分:0)
步骤1:首先解析JSONObject,假设您已将此JSON作为响应,并且您无法解析它。
JSONObject jsonObj = new JSONObject(mJsonString);
// mJsonString is the json you have received as a response
String errstr = jsonObj.getString("errstr");
String success = jsonObj.getString("success");
String data = jsonObj.getString("data");
JSONObject jsonObjData = new JSONObject(data);
String topFree = jsonObjData.getString("topFree");
if(topFree != null){
JSONArray mArray = new JSONArray(topFree);
for(int i=0;i<mArray.length;i++){
JSONObject mJsonObj = new JSONObject(
mArray.getString(i));
String appId = mJsonObj.getString("AppID");
String title = mJsonObj.getString("Title");
String price = mJsonObj.getString("Price");
String rating = mJsonObj.getString("Rating");
}
String topPaid = jsonObjData.getString("topPaid");
if(topPaid != null){
JSONArray mArrayTopPaid = new JSONArray(topPaid);
for(int i=0;i<mArrayTopPaid.length;i++){
JSONObject mJsonObj = new JSONObject(
mArrayTopPaid.getString(i));
String appId = mJsonObj.getString("AppID");
String title = mJsonObj.getString("Title");
String price = mJsonObj.getString("Price");
String rating = mJsonObj.getString("Rating");
}
String magazine = jsonObjData.getString("magazine");
if(magazine != null){
JSONArray mArrayMagazine = new JSONArray(magazine);
for(int i=0;i<mArrayMagazine.length;i++){
JSONObject mJsonObj = new JSONObject(
mArrayMagazine.getString(i));
String appId = mJsonObj.getString("AppID");
String title = mJsonObj.getString("Title");
String price = mJsonObj.getString("Price");
String rating = mJsonObj.getString("Rating");
}
String books = jsonObjData.getString("books");
String onlineApp = jsonObjData.getString("onlineApp");
}