How to Parse JSON String in Android

How to parse json string in Android?

Use JSON classes for parsing e.g

JSONObject mainObject = new JSONObject(Your_Sring_data);
JSONObject uniObject = mainObject.getJSONObject("university");
String uniName = uniObject.getString("name");
String uniURL = uniObject.getString("url");

JSONObject oneObject = mainObject.getJSONObject("1");
String id = oneObject.getString("id");
....

Android - how to parse JsonArray from string?

Hi @Caerulius, Harish, ρяσѕρєя K, Hot Licks , and all.
Finally, after 2 days of headache and 2 sleepless nights, I solved the issue. And because you spend your valued time to discuss with me, I see that I have to tell you the root cause. That's my responsibility.

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

As I told in my question, I used final String result = EntityUtils.toString(entity); to get JSON string from HttpEntity object. I have used this many times in the past and it worked. No problem. But, in this case, it's not.
The original JSON string like this:

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

But what I got like this:

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

This string is similar with the constant string which we may declare as below:

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

It's a valid string, but not a valid JSON String.

To fix this issue, I change the way to get JSON string, and remove unnecessary characters like this:

                            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 contain JSON string which I can parse correctly. I think this should a bug of HttpEntity library.

Hope this helps some other guys.

how to parse json string in android

You can use JSONObject in android.

    JSONParser jParser = new JSONParser();

// getting JSON string from URL
String jsonString = jParser.getJSONFromUrl(url).tostring();

JSONArray json= new JSONArray(jsonString);



try {

// looping through All Contacts
for(int i = 0; i < json.length(); i++){
JSONObject contacts = json.getJSONObject(i);

JSONObject user = contacts.getJSONObject("user");

String imageUrl = user.getString("profile_image_url");
String screenName = user.getString("Screen_name");

}
}catch(Exception e)
{
}

Parsing JSON String in Android Studio

It seems that the JSON you are trying to parse is not a JSON object (i.e. {...}) but merely a JSON string (i.e. "..."), because the quotes seem escaped (i.e. \" instead of ").

For instance this is a valid JSON string, but it is not a valid JSON object:

"{\"data\":[{\"temperaturaussen\":12,\"feuchtaussen\":77.41}]}"

while this is a valid JSON object:

{"data":[{"temperaturaussen":12,"feuchtaussen":77.41}]}

How to parse un json string into a List with MOSHI

You can't treat this json as a list. It's not a list itself but actually is a json object that contains a list.

To solve that, first build a class to wrap the "list":

@JsonClass(generateAdapter = true)
data class Wrapper(@Json(name = "list") val list: List<PaymentRequest>)

Then you are good to go:

val moshi = Moshi.Builder()
.add(KotlinJsonAdapterFactory())
.build()
val adapter = moshi.adapter<Wrapper>(Wrapper::class.java)
val paymentRequests = adapter.fromJson(response.body!!.source())!!.list


Related Topics



Leave a reply



Submit