2

I am receiving a JSON object from a PHP get request. It looks like this:

data == {"user_id":"7","emp_type_id":[{"0":"11","1":"10"}]}

I need to access the items within emp_type_id ("11" and "10" in this case).

I am able to access user_id just fine using data["user_id"] but can't figure out how to get the other items. I tried variations of:

eti=data["emp_type_id"];
var myVar = eti[0];

I'll want to loop through all items in emp_type_id eventually and add them to a JS array.

5 Answers 5

2

use a for in loop

var data = {"user_id":"7","emp_type_id":[{"0":"11","1":"10"}]};
var eti = data["emp_type_id"];
var myVar = eti[0];
for(var entry in myVar){
  alert(entry + ":\t" + myVar[entry] );
}

Running JSBIN Example

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

1 Comment

This will work, but I think it's best if he fixes his JSON like Hogan suggested in his post.
1

Neal's answer will help, but I think you have other problems. Your json structure looks wrong

a) data == {"user_id":"7","emp_type_id":[{"0":"11","1":"10"}]}

should it be

b) data == {"user_id":"7","emp_type_id":["11","10"]}

That is in english

a) emp_type_id an array of one object which has two fields 0 and 1?

b) emp_type_id an array of two items with values 11 and 10

If it should be b) then your json is wrong.

7 Comments

You might want to rephrase that - the JSON isn't 'wrong', it might be unnecessary, inefficient or complex, but it's certainly valid.
hmm i guess it should be b). i actually had that to start but then when i couldn't figure out how to get those values (thought they might need to have a key), i thought it was wrong and changed it to the other version (json_encode($emp_type_id, JSON_FORCE_OBJECT); vs. json_encode($emp_type_id);). does this change how i access those values?
@no.go - OK I changed it "+structure" activated.
It's not wrong syntactically, but the OP might have had something else in mind ...
@vee -- yes it does! If it is an array, as shown in b) then it it just is emp_type_id[x] for item x. If not then it gets crazy. See @James answer for more on this.
|
1

data is an object that has two properties, user_id and emp_type_id. emp_type_id is an array with one element (???), that element is an object with the properties 0 and 1.

There are two ways of reading a property from an object:

var prop = data['emp_type_id'];
var prop = data.emp_type_id;

If you have "weird" property names then you will have to use the square-bracket technique.

so

var prop0 = data.emp_type_id[0].0;
var prop1 = data.emp_type_id[0].1;

I'm concerned that "0" and "1" will fall under the category of "weird property names" so you might need to do

var prop0 = data.emp_type_id[0]["0"];
var prop1 = data.emp_type_id[0]["1"];

1 Comment

+1 - This answer makes it clear what was semantically wrong with the json he was using.
0

Try this:

var eti = data.emp_type_id;
var myVar = eti[0];

Comments

0
var eti = data.emp_type_id;
var myVar = eti[0];

for(var key in myVar){
    myVar[key];
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.