0

I have following json encoded array coming from ajax call

{"country":{"0":"United States of America","United States of America":{"states":{"0":"Alaska","Alaska":{"cities":["Adak","Akiachak","Akiak","Akutan","Alakanuk"]}}}}}

Following is my ajax code

$.ajax({
   method: "POST",
   url: "test.php",
   data: "action=2",
   cache: 'false',
   success: function(abcd){
     alert(abcd);
     var obj = new Array();
     var obj = $.parseJSON(abcd);
     alert(obj.country.length);
   }
});

When i try using obj.country[0] it returns "United States of America". But when i try to get the length of the array using obj.country.length it returns undefined.

I have browsed a couple of posts and only difference i could see was use of dataType: json and using header() to define content type to json on .php page. I have tried both methods but that didn't work out either. And on .php page i have declared following as array:

$data = array();
$data['country'] = array();
$data['country']['United States of America'] = array();
$data['country']['United States of America']['states'] = array();
$data['country']['United States of America']['states']['Alaska']['cities'] = array();

2 Answers 2

3

This is because obj.country isn't an array, but an object.

An object is enclosed in curly braces ({}) and an array in square ones ([]). See http://json.org/ for more information.

In your example content, "cities":["Adak","Akiachak","Akiak","Akutan","Alakanuk"] would be an array, which you can call with obj["country"]["United States of America"]["states"]["Alaska"]["cities"].


So, judging from your example this would work:

$us['name'] = "United";
$alaska['name'] = "Alaska";
$alaska['cities'] = array("city1", "city2");
$us['states'] = array($alaska)
$data = array($us);

I expect it would render like [{"name": "United", "states": [{"name":"Alaska", "cities": ["city1","city2"]}]}].

I'm sure it could be done better or more compact though, I don't have much experience with PHP in this regard.

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

11 Comments

Ok so how do I convert it to any array?
You might want to add, that obj.country[0] actually returns a value, because there is an attribute called 0 within said object.
@SaadBashir If you can't change the source of the content, you will have to loop through the object and add every entry into a new array.
I can change the source of the content. But like I said I am already defining it as an array like I explained in the question. At every stage I have defined it as an array. I don't know what am I doing wrong here.
@SaadBashir: In you PHP code you are overwritting the array definition with every assignment. If you encode it as JSON it will be an object, because Javascript does not know hashed arrays.
|
0

What about this?

alert((Object.keys(obj.country) || []).length);

2 Comments

What about that? Add more information to your answer. What are you trying to accomplish with that? How is that helping OP?
Well yes this returns 2.

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.