How to parse JsonArray from a string?

I am trying to parse a json array from a json string, but it always throws a data of type java.lang.String cannot be converted to JSONArray .

Please tell me if I am wrong.

Thanks.

Here are my codes to get Json from the server:

 try { String url = String.format(<url here>, province.provinceCode2); HttpClient httpClient = getHttpClient(); HttpGet httpGet = new HttpGet(url); HttpResponse httpResponse = httpClient.execute(httpGet); HttpEntity entity = httpResponse.getEntity(); final String result = EntityUtils.toString(entity); parseAndSaveJsonData(province, result); } catch (Exception e) { e.printStackTrace(); } 

Here are the codes for parsing JsonArray:

 String jsonString = <below json string> JSONArray ja = new JSONArray(jsonString); 

Here is my json line:

  [ { "LotPrizes":[ { "Prize":"Giảitám", "Range":"50" }, { "Prize":"Giảibảy", "Range":"264" }, { "Prize":"Giảisáu", "Range":"3654-5162-3097" }, { "Prize":"Giảinăm", "Range":"9739" }, { "Prize":"Giảitư", "Range":"97690-99274-32442-69432-04855-10132-17085" }, { "Prize":"Giảiba", "Range":"73745-13007" }, { "Prize":"Giảinhì", "Range":"05521" }, { "Prize":"Giảinhất", "Range":"74870" }, { "Prize":"GiảiDB6", "Range":"878833" } ] }, { "LotPrizes":[ { "Prize":"Giảitám", "Range":"50" }, { "Prize":"Giảibảy", "Range":"264" }, { "Prize":"Giảisáu", "Range":"3654-5162-3097" }, { "Prize":"Giảinăm", "Range":"9739" }, { "Prize":"Giảitư", "Range":"97690-99274-32442-69432-04855-10132-17085" }, { "Prize":"Giảiba", "Range":"73745-13007" }, { "Prize":"Giảinhì", "Range":"05521" }, { "Prize":"Giảinhất", "Range":"74870" }, { "Prize":"GiảiDB6", "Range":"878833" } ] } ] 
+6
source share
3 answers

Hi @Caerulius, Harish, ρσѕρє K, Hot Licks and all that. Finally, after two days of headache and two sleepless nights, I solved the problem. And since you spend your precious time discussing with me, I see that I have to tell you the root cause. That is my responsibility.

First of all, I am a senior Android developer. So, at least I know about JSON basic, I know how to parse data from a JSON string, and I know many useful online tools for checking it. I confirm that the JSON string received from the server is valid.

As I said in my question, I used final String result = EntityUtils.toString(entity); to get a JSON string from an HttpEntity object. I have used this many times in the past and it worked. No problems. But in this case it is not so. Original JSON String:

  [{ "LotPrizes":[ { "Prize":"Giảitám", "Range":"50" }, { "Prize":"Giảibảy", "Range":"264" }, ... }] 

But what I got:

 "[{ \"LotPrizes\":[ { \"Prize":\"Giảitám\", \"Range\":\"50\" }, { \"Prize\":\"Giảibảy\", \"Range\":\"264\" }, ... }]" 

This line is like a constant line, which we can declare below:

 String stringVariable = "\"[{ \\\"LotPrizes\\\":[ { \\\"Prize":\\\"Giảitám\\\", \\\"Range\\\":\\\"50\\\" }, { \\\"Prize\\\":\\\"Giảibảy\\\", \\\"Range\\\":\\\"264\\\" }, ... }]\" ; 

This is a valid string, but not a valid JSON string.

To fix this problem, I change the way I get the JSON string and delete the unnecessary characters as follows:

  HttpClient httpClient = getHttpClient(); HttpGet httpGet = new HttpGet(url); HttpResponse httpResponse = httpClient.execute(httpGet); HttpEntity entity = httpResponse.getEntity(); InputStream is = entity.getContent(); BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"), 8); StringBuilder sb = new StringBuilder(); String line = null; while ((line = reader.readLine()) != null) { sb.append(line + "\n"); } is.close(); String json = sb.toString(); json = json.replace("\\", ""); json = json.substring(1); json = json.substring(0, json.length() - 2); 

Now the json variable contains a JSON string that I can parse correctly. I think this should be a HttpEntity library error.

Hope this helps some other guys.

+2
source

Here's how to initialize a JSON parser:

 JSONObject jsonObject = new JSONObject(jsonString); 

This will give you the whole string as a Json object. From there, pull out a separate array as a JsonArray, for example:

 JSONArray jsonArray = jsonObject.getJSONArray("LotPrizes"); 

To access each LotPrizes, you can use for loop logic:

 for(int i=0;i<jsonArray.length();i++) { JSONObject curr = jsonArray.getJSONObject(i); prize = curr.getString("Prize") //Do stuff with the Prize String here //Add it to a list, print it out, etc. } 

EDIT: Final code after editing JSON:

 JSONArray jsonArray = null; String jsonString = <your string> String currPrize = null; JSONObject jsonObject = new JSONObject(jsonString); jsonArray = jsonObject.getJSONArray("data"); for(int i=0;i<jsonArray.length();i++) { JSONArray currLot = jsonArray.getJSONObject(i); for(int j=0; j<currLot.length();j++) { JSONobject curr = currLot.getJSONObject(j); currPrize = curr.getString("Prize"); //Do something with Prize } } 

This code is functional, and I use an almost identical version in my code. Hope this (finally) works for you.

+14
source

You can get jsondata from your string as follows.

  JSONObject json = new JSONObject(jsonString); JSONArray jData = json.getJSONArray("data"); for (int i = 0; i < jData.length(); i++) { JSONObject jo = jData.getJSONObject(i); JSONArray jLotPrizes = jo.getJSONArray("LotPrizes"); for (int j = 0; j < jLotPrizes.length(); j++) { JSONObject jobj = jLotPrizes.getJSONObject(j); Log.i("Prize", "" + jobj.getString("Prize")); Log.i("Range", "" + jobj.getString("Range")); } } 
+1
source

All Articles