Reputation: 39
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());
}
Upvotes: 1
Views: 116
Reputation: 136
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();
}
Upvotes: 1
Reputation: 413
// 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.
Upvotes: 1