How to parse a large JSON file in android

I want to parse the JSON response by url, but I always had an error. I tried so much, but could not solve the problem. Please suggest me fix the errors.

My JSON answer:

{ "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 ..] 

My code is:

 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 need to parse information on all products.

+4
source share
3 answers

It is better to use gson parser if you have a large JSON file from the server. I also got an error while reading big JSON. Because JSON first loads the data into memory that parses it. therefore, a change to the memory exception may occur.

+2
source

you can use the open Gson.jar API to retrieve data from Json. It retrieves data in the standard order ... try for example ... http://blog.foos-bar.com/2010/08/parsing-facebook-json.html

+1
source

You must download the full JSON before you can parse it. This should help you in your project:

 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; } JSONObject jo = new JSONObject(str); JSONArray ja = jo.getJSONObject("products").getJSONArray("product"); for (int i = 0; i < ja.length(); i++) { Log.i("MyDebug", "value----" + ja.getJSONObject(i).getString("manufacturer")); } } 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(); } 

Remember to work with has("") if you don't know for sure that the key exists.

+1
source

All Articles