jsonobject cannot be converted to jsonarray nested jsonarray android - android

I want to parse a nested jsonarray
JSONParser jParser = new JSONParser();
// getting JSON string from URL
JSONObject json = jParser.getJSONFromUrl(url);
try {
dialogcreated = json.getJSONArray(TAG_DETAILS);
// Log.d("apptoken",login.toString());
for (int i = 0; i < dialogcreated.length(); i++) {
JSONObject d = dialogcreated.getJSONObject(i);
String msg = d.getString(TAG_MSG);
dialogs = d.getJSONArray("dialogdetails");
I am getting jsonobject cannot be converted to jsonarray error on
dialogs = d.getJSONArray("dialogdetails"); line
my json array
{"dialog_details":[{"msg":"success","dialogdetails":{"dialog_id":"139","dialog_category":"2","dialog_title":"apptesting1","dialog_description":"apptesting2","dialog_image":null,"dialog_type":"P","mem_id":"27","temp_moderator_assigned":"0","moderator_assigned":"0","created_on":"6 minutes ago","updated_by":null,"updated_on":"6 minutes ago","mem_dialog_type":"P","published":"0","dialog_status":"1","dialog_archived":"0","dialog_votes":"0","featured":"0","dialog_members":"0","dialog_posts":"0","wtavg":"0","d_member_username":"icube2","d_mem_id":"27","d_member_avatar":"0","d_member_email":"icube1solutions#gmail.com","d_category_id":"2","d_category_name":"Games"}}]}

{ // json object
"dialog_details": [ // json array
{
"msg": "success",
"dialogdetails": { // json object
"dialog_id": "139",
dialogdetails is not a JSONArray its a JSONObject
Change to JSONObject
dialogs = d.getJSONArray("dialogdetails");
to
JSONObject dialogs = d.getJSONObject("dialogdetails");
[ represents json array node
{ represetns json object node
JSONObject jb = new JSONObject("myjsonstring");
JSONArray jr = (JSONArray)jb.getJSONArray("dialog_details"):
for(int i=0;i<jr.length();i++)
{
JSONObject d = (JSONObject) jr.getJSONObject(i);
String msg = d.getString(TAG_MSG);
JSONObject dialogs = d.getJSONObject("dialogdetails");
String dialog_id= dialogs.getString("dialog_id");
}

dialogdetails is not JsonAray..
And you are doing wrong here
dialogs = d.getJSONArray("dialogdetails");//Wrong
Its a JsonObject
dialogs = d.getJSONObject("dialogdetails");
JSON String start with braces { stands for JsonObject
"dialogdetails": {
"dialog_id": "139",
"dialog_category": "2",
"dialog_title": "apptesting1",
"dialog_description": "apptesting2",
"dialog_image": null,
"dialog_type": "P",
"mem_id": "27",
"temp_moderator_assigned": "0",
"moderator_assigned": "0",
"created_on": "6 minutes ago",
"updated_by": null,
"updated_on": "6 minutes ago",
"mem_dialog_type": "P",
"published": "0",
"dialog_status": "1",
"dialog_archived": "0",
"dialog_votes": "0",
"featured": "0",
"dialog_members": "0",
"dialog_posts": "0",
"wtavg": "0",
"d_member_username": "icube2",
"d_mem_id": "27",
"d_member_avatar": "0",
"d_member_email": "icube1solutions#gmail.com",
"d_category_id": "2",
"d_category_name": "Games"
}

Related

Error while parsing JSON Object in Java Android

In my Android app, I am fetching the details of Points table of a tournament to convert the output to a string from JSON object to Java
JSON object is shown below:
{
"group": {
"Teams": [
{
"name": "Team 1",
"p": "10",
"w": "9",
"l": "1",
"points": "18"
},
{
"name": "Team 2",
"p": "10",
"w": "9",
"l": "1",
"points": "18"
},
{
"name": "Team 3",
"p": "10",
"w": "9",
"l": "1",
"points": "18"
},
{
"name": "Team 4",
"p": "10",
"w": "6",
"l": "4",
"points": "12"
},
{
"name": "Team 5",
"p": "10",
"w": "6",
"l": "4",
"points": "12"
},
{
"name": "Team 6",
"p": "10",
"w": "6",
"l": "4",
"points": "12"
},
{
"name": "Team 7",
"p": "10",
"w": "5",
"l": "5",
"points": "11"
},
{
"name": "Team 8",
"p": "10",
"w": "5",
"l": "5",
"points": "11"
}
]
}
}
Android Java Code is below:
JSONObject match = new JSONObject(response);
if (match.has("group")) {
JSONObject group = match.getJSONObject("group");
if (match.has("Teams")) {
JSONObject teams = group.getJSONObject("Teams");
if (teams.has("0")) {
JSONObject teams_object = teams.getJSONObject("0");
String team_name = teams_object.getString("name");
String matches_played = teams_object.getString("p");
String matches_won = teams_object.getString("w");
String matches_lost = teams_object.getString("l");
String points = teams_object.getString("points");
}
}
}
But I am getting the error where I print the error message through getMessage() method. Here is the error below:
Error: Value ["name","p","w","l","points"] at header of type org.json.JSONArray cannot be converted to JSONObject
Can anyone please help like where I am going wrong or what is the fix ? Thanks in advance
In your Json Teams holds the Array of Object and you are parsing wrong.
Try this
JSONObject jsonObject = new JSONObject(response);
JSONObject groups = jsonObject.getJSONObject("group");
JSONArray teams = groups.getJSONArray("Teams");
for(int i=0;i<teams.length();i++){
JSONObject obj = teams.getJSONObject(i);
name.append(obj.getString("name")+"\n");
}
There is 4 mistakes in your code,
1.You have to check whether group has teams instead match has teams.
2.You have to get team as JSONArray instead of JSONObject because it is array.
3.You have to check the size of team, instead of find the object with key 0, there is no object with name 0 in your group,
4.You have to get the object based on index instead of key(ie., 0)
Please check the working code, this is tested
try {
JSONObject match = new JSONObject(response);
if (match.has("group")) {
JSONObject group = match.getJSONObject("group");
if (group.has("Teams")) {
JSONArray teams = group.getJSONArray("Teams");
if (teams.length() > 0) {
JSONObject teams_object =(JSONObject) teams.get(0);
String team_name = teams_object.getString("name");
String matches_played = teams_object.getString("p");
String matches_won = teams_object.getString("w");
String matches_lost = teams_object.getString("l");
String points = teams_object.getString("points");
Log.v(TAG, team_name);
}
}
}
} catch (JSONException e) {
e.printStackTrace();
}
try {
JSONObject jsonObject = new JSONObject(response.body().string());
JSONObject subObject = jsonObject.getJSONObject("group") ;
JSONArray jsonArray = subObject.getJSONArray("Teams");
for (int i=0; i<jsonArray.length(); i++){
JSONObject object = jsonArray.getJSONObject(i);
String name = jsonObject.getString("name");
String p= jsonObject.getString("p");
String w= jsonObject.getString("w");
String l= jsonObject.getString("l");
String points = jsonObject.getString("points");
}
} catch (Exception e) {
e.printStackTrace();
}
if (match.has("group")) {
JSONObject group = match.getJSONObject("group");
if (match.has("Teams")) {
JSONArray teams = group.getJSONArray("Teams");
// for only 0th element use below code else looping
JSONObject teams_object = (JSONObject) teams.get(0);
String team_name = teams_object.getString("name");
String matches_played = teams_object.getString("p");
String matches_won = teams_object.getString("w");
String matches_lost = teams_object.getString("l");
String points = teams_object.getString("points");
}
}
Error: Value ["name","p","w","l","points"] at header of type
org.json.JSONArray cannot be converted to JSONObject
You are getting the error because there is an Array of Teams and you are trying parse with JSONObject
Please check below code snippet. It will work.
try {
JSONObject match = new JSONObject("your_response");
if (match.has("group")) {
JSONObject group = match.getJSONObject("group");
if (group.has("Teams")) {
JSONArray teams = group.getJSONArray("Teams");
for(int i=0; i < teams.length(); i++){
JSONObject teams_object = (JSONObject) teams.get(i);
String team_name = teams_object.getString("name");
String matches_played = teams_object.getString("p");
String matches_won = teams_object.getString("w");
String matches_lost = teams_object.getString("l");
String points = teams_object.getString("points");
}
}
}
} catch (JSONException e) {
e.printStackTrace();
}
Try this #SaAsh, I have just edited by taking your answer
if (match.has("group")) {
JSONObject group = match.getJSONObject("group");
if (match.has("Teams")) {
JSONObject teams = group.getJsonArray("Teams");
for(int i = 0 ; i < teams.length();i++){
JSONObject teams_object = teams.getJSONObject("i");
String team_name = teams_object.getString("name");
String matches_played = teams_object.getString("p");
String matches_won = teams_object.getString("w");
String matches_lost = teams_object.getString("l");
String points = teams_object.getString("points");
}
}
}
}

How do we pass JSONObject as string in getJSONArray();? [duplicate]

This question already has answers here:
How to iterate over a JSONObject?
(15 answers)
Closed 5 years ago.
I want to fetch multiple JSONArray from an JSONObject
JSONObject jsonObject=new JSONObject(response_view);
for(int x=0; x<jsonObject.length(); x++) {
JSONArray jsonArray = jsonObject.getJSONArray("Here I have to pass");
}
This is my JSON,
{
"Mr. VICKRAM SINGH_269": [{
"status": "P",
"date": "2017-02-05"
}, {
"status": "P",
"date": "2017-02-06"
}],
"Mr. VIVEK KUMAR YADAV_276": [{
"status": "P",
"date": "2017-02-05"
}, {
"status": "P",
"date": "2017-02-06"
}]
}
you can parse Json like below:
try{
JSONObject json = new JSONObject(jsonRespondeString);
Iterator<String> iterator = json.keys();
while (iterator.hasNext()){
String key = iterator.next();
JSONArray jsonArray = json.getJSONArray(key);
for(int i=0;i<jsonArray.length();i++){
JSONObject object = jsonArray.getJSONObject(i);
String value1 = object.getString("key1");
String value2 = object.getString("key2");
}
}
}
catch (JSONException e){
e.printStackTrace();
}
you can get name and id from key like this:
String str="Mr. VICKRAM SINGH_269";
String array[]= str.split("_");
String name =array[0];
String id =array[1];
JSONArray jsonArray=jsonObject.getJSONArray("Write the name of your array here");
and then loop your jsonArray Object

Parsing json (android) [duplicate]

Before I parse json json array, but this json object. Its It drove me to a standstill. How i can parse json like this:
{ "test1": {
"1": {
"action": "0",
"type": "1",
"id": "1",
},
"2": {
"action": "0",
"type": "1",
"id": "2",
}
},
"test2": {
"1": {
"id": "1",
"name": "one"
},
"2": {
"id": "2",
"name": "two"
},
"5": {
"id": "5",
"name": "three"
}
}}
When you don't have a fixed set of keys, that you know upfront, the only way to parse it is to use keys(). It returns an Iterator with the keys contained in the JSONObject. In your case you could have
JSONObject jsonObject = new JSONObject(...);
Iterator<String> iterator = jsonObject.keys();
while(iterator.hasNext()) {
String currentKey = iterator.next();
JSONObject obj = jsonObject.optJSONObject(key);
if (obj != null) {
Iterator<String> iterator2 = obj.keys();
}
}
iterator will return test1 and test2, while iterator2 will return 1 and 2, for test1 and 1 ,2 , 5 for test2
You can create a JSONObject From string as bellow
JSONObject jsonObject = new JSONObject(YOUR_JSON_STRING);
and to parse the jsonObject
JSONObject test1Json = jsonObject.getJSONObject("test1");
JSONObject oneTest1Json = test1Json.getJSONObject("1");
to get String values
String action = oneTest1Json.getString("action");
String type = oneTest1Json.getString("type");
String id = oneTest1Json.getString("id");
Log.d("Json parse","action -"+action+" type -"+type+" id -"+id);
if need them as JSONArray the you can try
public JSONArray getJsonArray (JSONObject jsonObject){
JSONArray nameJsonArray = jsonObject.names();
JSONArray jsonArray = new JSONArray();
for (int i = 0; i < nameJsonArray.length(); i++) {
try {
String key = nameJsonArray.getString(i);
jsonArray.put(jsonObject.getString(key));
} catch (JSONException e) {
e.printStackTrace();
}
}
return jsonArray;
}
in case keys like test1,test2,test3...
JSONObject jsonObject = new JSONObject("{'test1':'Kasun', 'test2':'columbo','test3': '29'}");
JSONArray jsonArray = new JSONArray();
for (int i = 1; i <= jsonObject.names().length(); i++) {
try{
jsonArray.put(jsonObject.getString("test" + i));
}catch (JSONException e){
e.printStackTrace();
}
you can get your JSONArray this way.

How to parse a JSON Object to get an Array with in Array?

This is my JSON output. I am trying to return the "id": "9040" from instructions but am having trouble
{
"packages": [
{
"instructions": [
{
"id": "9040",
"fn": "test.xml",
"#type": "down",
"fp": ""
}
],
"id": "47968",
"time": 1396036698630,
"priority": 0
}
]
}
Here is my code that returns the "Package" -> "Id"
JSONObject jObject = new JSONObject(json);
JSONArray jsonMainNode = jObject.optJSONArray("packages");
int lengthJsonArr = jsonMainNode.length();
for(int i=0; i < lengthJsonArr; i++)
{
/****** Get Object for each JSON node.***********/
JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);
fileid = Integer.parseInt(jsonChildNode.optString("id").toString());
if (fileid > 0 )
Log.i("JSON parse", "id = "+ fileid);
}
Any help would be much appreciated.
Thank you
You forget about JSONArray instructions
{
"packages": [
{
"instructions": [
{
"id": "9040",
"fn": "test.xml",
"#type": "down",
"fp": ""
}
],
"id": "47968",
"time": 1396036698630,
"priority": 0
}
]
}
To parse
JSONObject jObject = new JSONObject(json);
JSONArray jsonMainNode = jObject.optJSONArray("packages");
JSONObject jb = (JSONObject) jsonMainNode.get(0);
JSONArray instructions = jb.getJSONArray("instructions");
JSONObject jb1 = (JSONObject) instructions.get(0);
String id = jb1.getString("id");

How to parse nested JSON object using the json library?

i want to parse the json object using json library.
{
"batters":
{
"batter":
[
{ "id": "1001", "type": "Regular" },
{ "id": "1002", "type": "Chocolate" },
{ "id": "1003", "type": "Blueberry" },
{ "id": "1004", "type": "Devil's Food" }
]
}
}
Using JSON..
JSONObject object = new JSONObject(yourString);
JSONObject batters = object.getJSONObject("batters");
JSONArray batter = batters.getJSONArray("batter");
for(int i = 0 ; i < batter.length() ; i++) {
JSONObject object1 = (JSONObject) batter.get(i);
String id = object1.getString("id");
}
{
"result": "success",
"countryCodeList":
[
{"countryCode":"00","countryName":"World Wide"},
{"countryCode":"kr","countryName":"Korea"}
]
}
Here below I am fetching country details
JSONObject json = new JSONObject(jsonstring);
JSONArray nameArray = json.names();
JSONArray valArray = json.toJSONArray(nameArray);
JSONArray valArray1 = valArray.getJSONArray(1);
valArray1.toString().replace("[", "");
valArray1.toString().replace("]", "");
int len = valArray1.length();
for (int i = 0; i < valArray1.length(); i++) {
Country country = new Country();
JSONObject arr = valArray1.getJSONObject(i);
country.setCountryCode(arr.getString("countryCode"));
country.setCountryName(arr.getString("countryName"));
arrCountries.add(country);
}

Categories

Resources