1

I am getting this error on parsing json. Can anyone help me to solve this.

  {
"website": [
    "http://www.example.com",
    "https://buy.example.com"
],
"number": [
    "4546",
    "54256456"
],
"email": [
    "[email protected]",
    "[email protected]"
],
"address": [
    "any adddress here",
    "Address 2"
]
  }

Anyone help me to how exactly parse this json...

Json Parsing code

             String JSON_STRING = response.body().string();
                    Log.e("json_string", JSON_STRING.toString());
                    //Toast.makeText( getApplicationContext(), "Response : "+JSON_STRING, Toast.LENGTH_LONG ).show();

                    JSONObject object = new JSONObject( JSON_STRING );
                    JSONArray jsonArray = object.getJSONArray( "website" );

                        for (int i=0; i<jsonArray.length(); i++)
                        {
                            JSONObject website = jsonArray.getJSONObject( i );
                            Log.e("website", website.toString());
                        }
1
  • Please post JSON parsing code. Commented Apr 5, 2019 at 5:38

2 Answers 2

1

try this code

 try {
        JSONObject jsonObject = new JSONObject(json);
        JSONArray websites = jsonObject.getJSONArray("website");
        for (int i = 0; i < websites.length(); i++) {
            Log.d("TAG", websites.getString(i));
        }

    } catch (Exception e) {
        e.printStackTrace();
    }
Sign up to request clarification or add additional context in comments.

1 Comment

Yes, i got the answer and i commented below.. Your answer is also correct i used the same approach...
1
// for email array and response variable is your jsonObject coming from server
JSONArray jsonArray = response.getJSONArray("email")
for(int i = 0; i < jsonArray.length(); i++){
   //get email one by one 
   String email = jsonArray.getString(i)
}

Let me know if it is working.

2 Comments

JSONObject object = new JSONObject( response.body().string() ); JSONArray jsonArray = object.getJSONArray("email"); for(int i = 0 ;i < jsonArray.length();i++){ String email = jsonArray.getString(i); Log.e("emailjson", email.toString()); }
ok i thought you are getting response from server in jsonObject not in string otherwise we have to convert string to jsonObject.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.