1

Hello I have this data:

var data={
"city_id": "2",
"dist": "2",
"City Name 1": [
{
"id": 1,
"name": "name 1",
"address": "address 1",
"tel": "tel 1",
"Radio": "0",
"surgery": "0"
},
{
"id": 2,
"name": "name 2",
"address": "address 2",
"tel": "tel 2",
"Radio": "0",
"surgery": "0"
}
],{
"city_id": "2",
"dist": "2",    
"City Name 2": [
{
"id": 1,
"name": "name 1",
"address": "address 1",
"tel": "tel 1",
"Radio": "0",
"surgery": "0"
},
{
"id": 2,
"name": "name 2",
"address": "address 2",
"tel": "tel 2",
"Radio": "0",
"surgery": "0"
}
]
}
};

I want to loop through this JSON data to extract City_id, dist_id, City Name, and then another loop to get name and address and telephone. The data format is not correct, I don't know how to fix it.

I appreciate your help. Thank you

3
  • at first) this is not valid json but js object. You can use "for" function with "in". w3schools.com/js/js_loop_for.asp Commented Sep 14, 2015 at 22:38
  • What will be the output, an array? Commented Sep 15, 2015 at 0:16
  • @BerozaPaul No, I want to display the data in a table Commented Sep 15, 2015 at 10:04

1 Answer 1

1

Your brackets are off making this not a valid json.

var data={ //------------------------------------open
"city_id": "2",
"dist": "2",
 "City Name 1": [
{//----------------------open
"id": 1,
"name": "name 1",
"address": "address 1",
"tel": "tel 1",
"Radio": "0",
"surgery": "0"
},//----------------------close
{//-----------------------open
"id": 2,
"name": "name 2",
"address": "address 2",
"tel": "tel 2",
"Radio": "0",
"surgery": "0"
}//----------------------close
],{//----------------------------------open (problem is here. There is nothing index this object}
"city_id": "2",
"dist": "2",    
"City Name 2": [
{//-----------------------open
"id": 1,
"name": "name 1",
"address": "address 1",
"tel": "tel 1",
"Radio": "0",
"surgery": "0"
},//----------------------close
{//-----------------------open
"id": 2,
"name": "name 2",
"address": "address 2",
"tel": "tel 2",
"Radio": "0",
"surgery": "0"
}//----------------------close
]
}//----------------------------------close
}; //------------------------------------------------close

That bit needs to be in the array or removed from the json.

Sign up to request clarification or add additional context in comments.

1 Comment

I could not fix it. Is there a way to do it !

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.