0

嗨,我这里有一个脚本

http://myprocity.com/android/fetchthecity.php

这将返回一个数组数组。我想在 Android 中解析这个并存储每个值。

我现在的代码是这样的,也许这是有缺陷的?我什么都没有出现

public static void refreshFeed(Activity act){

    ActionEngine.swamiOn = false;

    FetchTheCityTask f = new FetchTheCityTask(act);
    f.execute();

    try {

        if (f.get() != null) {

            JSONArray feedArr = new JSONArray();
            feedArr = json.getJSONArray("data");
            ArrayList<UserRecord> items = new ArrayList<UserRecord>();

            for(int i = 0; i < feedArr.length(); i++){

                for(int j = 0; j < feedArr.getJSONArray(i).length(); j++) {

                    JSONObject row = feedArr.getJSONArray(i).getJSONObject(j);
                    String item = row.getString("Item");
                    String descp = row.getString("Description");
                    String pic = row.getString("PicPath");
                    String time = row.getString("Time");
                    String donatedby = row.getString("DonatedBy");
                    String ebase = row.getString("Ebase");
                    String cond = row.getString("Condition");
                    String loc = row.getString("Location");

                    items.add(new UserRecord(item,descp,pic,time,donatedby,
                            ebase,cond,loc));

                }
            }

            TheCityFragment.feedArea.setAdapter(new UserItemAdapter(act,
                    android.R.layout.simple_list_item_1, items));
4

2 回答 2

0

Your JSON result, parsed on : http://bodurov.com/jsonformatter/ shows this result :

JSON Result

I'd recommend to change the json result to this one :

{"data" : [yourjsonArray]}

So you need to encapsulate the JSON_ARRAY inside a JSON_OBJECT, so you can use getJSONArray("data") to get the array.

JSONObject jObject = new JSONObject(resultstringhere); //f.get() ?
JSONArray jArray = jObject.getJSONArray("data");

Hope this helps,

Reid

于 2013-09-18T04:00:25.140 回答
0

这将返回一个数组数组。

我不这么认为,

我从您的文件“对象数组”中看到,因此您的代码不起作用。

在此处输入图像描述

尝试做一些事情:

JSONArray json = new JSONArray(resultAsString);
// ...

for(int i=0;i<json.length();i++){ 
              //...
               JSONObject row = json.getJSONObject(j);
                String item = row.getString("Item");
                String descp = row.getString("Description");
                String pic = row.getString("PicPath");
                String time = row.getString("Time");
                String donatedby = row.getString("DonatedBy");
                String ebase = row.getString("Ebase");
                String cond = row.getString("Condition");
                String loc = row.getString("Location"); 
          //..         
}

希望对你有帮助

于 2013-09-17T21:50:55.827 回答