如何解析Android的一个大的JSON文件文件、Android、JSON

2023-09-04 12:22:32 作者:谁的青春不折腾

我想从解析URL JSON响应,但我总是得到一个错误。我试过这么多,但未能解决问题。请建议我去纠正错误。

I want to parse a JSON response from URL, but I got always an error. I am tried so much but failed to resolve the problem. Please suggest me to rectify the errors.

我的JSON响应:

 {
 "classification": {
     "relevancyScore": 999,
     "searchUrl": {
         "value": "http://www.bizrate.com/iphone-cases/index__rf--af1__af_assettype_id--10__af_creative_id--6__af_id--50085__af_placement_id--1.html"
     }
 },
 [.. hundreds of lines removed ..]

我的code:

My code:

public class test extends Activity {

    /** Called when the activity is first created. */
    @SuppressWarnings({ "rawtypes", "unchecked" })
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        ListView lv = (ListView)findViewById(R.id.listView1);

        lv.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, this.fetchTwitterPublicTimeline()));        
    }

    public ArrayList<String> fetchTwitterPublicTimeline()
    {
        ArrayList<String> listItems = new ArrayList<String>();

        try {
            URL twitter = new URL(
                    "http://catalog.bizrate.com/services/catalog/v1/us/product?publisherId=50085&placementId=1&categoryId=1&keyword=iphone+cases&start=0&results=10&sort=relevancy_desc&brandId=&attFilter=&zipCode=90291&biddedOnly=&minRelevancyScore=100&apiKey=58f536aa2fab110bbe0da501150bac1e&format=json");
            URLConnection tc = twitter.openConnection();
            BufferedReader in = new BufferedReader(new InputStreamReader(
                    tc.getInputStream()));

            String line;
            String str;
            while ((line = in.readLine()) != null) {
                //str = line.substring(line.indexOf("{"), line.lastIndexOf(line));
                //str = line.replaceAll("BIZRATE.Suggest.callback(", null);
                //str = str.replaceAll(")", null);
                //System.out.println("SubString:"+str);
                JSONArray ja = new JSONArray(line);

                for (int i = 0; i < ja.length(); i++) {
                    JSONObject jo = (JSONObject) ja.get(i);
                    System.out.println("value----"+jo.getJSONObject("product"));
                    //listItems.add(jo.getString("suggestions"));
                }
            }
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return listItems;
    }
}

我要解析的所有产品的信息。

I have to parse information of all products.

推荐答案

更​​好地利用GSON解析器,如果你有一个从服务器的一个大的JSON文件。我也得到了内存不足的错误,而阅读大JSON.Because JSON首先将数据加载到内存中解析它。所以有可能走出内存异常的变化。

Better use gson parser if you have a big JSON file from server. I also got out of memory error while reading big JSON.Because JSON first loads data into memory that parse it . so there may be change of getting out of memory exception.