Welcome to WebmasterWorld Guest from 54.227.110.209

Forum Moderators: open

Message Too Old, No Replies

JSON accessing object with numeric index

     

camilord

12:35 am on Nov 22, 2012 (gmt 0)

5+ Year Member




var jsn = {"3":{"334":[{"skin_condition_question_answer_id":"957","case_details_answer_value":"fdfsdfsdf"}],"335":[{"skin_condition_question_answer_id":"1276","case_details_answer_value":"Right Upper Arm"}],"336":[{"skin_condition_question_answer_id":"966"}],"337":[{"skin_condition_question_answer_id":"968"}],"338":[{"skin_condition_question_answer_id":"970","case_details_answer_value":"fdsfsddfsfds"}],"374":[{"skin_condition_question_answer_id":"1079"}],"375":[{"skin_condition_question_answer_id":"1084"}],"376":[{"skin_condition_question_answer_id":"1090"}],"377":false}};

var jq = {"3":{"374":{"0":{"skin_condition_question_answer_id":"1076"}},"375":{"0":{"skin_condition_question_answer_id":"1082"}},"376":{"0":{"skin_condition_question_answer_id":"1090"}},"377":""}};


anyone can help me please... javascript guru out there.. please help..

how do i access the object?

is it?

alert(jsn[0]); // undefined
alert(jq[0]); // undefined

alert(jsn.3); // error
alert(jq.3); // error

anyone?

camilord

12:46 am on Nov 22, 2012 (gmt 0)

5+ Year Member



oooopppsss.. nevermind.. i got the answer... heheheh..


var jsn = {"3":{"334":[{"skin_condition_question_answer_id":"957","case_details_answer_value":"fdfsdfsdf"}],"335":[{"skin_condition_question_answer_id":"1276","case_details_answer_value":"Right Upper Arm"}],"336":[{"skin_condition_question_answer_id":"966"}],"337":[{"skin_condition_question_answer_id":"968"}],"338":[{"skin_condition_question_answer_id":"970","case_details_answer_value":"fdsfsddfsfds"}],"374":[{"skin_condition_question_answer_id":"1079"}],"375":[{"skin_condition_question_answer_id":"1084"}],"376":[{"skin_condition_question_answer_id":"1090"}],"377":false}};

var jq = {"3":{"374":{"0":{"skin_condition_question_answer_id":"1076"}},"375":{"0":{"skin_condition_question_answer_id":"1082"}},"376":{"0":{"skin_condition_question_answer_id":"1090"}},"377":""}};

function testJS() {
alert("PHP json_encode() -> " + jsn["3"]["334"]["0"]["skin_condition_question_answer_id"]);
alert("jQuery Object/Array ->" + jq["3"]["374"]["0"]["skin_condition_question_answer_id"]);
}

daveVk

6:19 am on Nov 22, 2012 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member



Dot notation should also work

jsn.3.334.0.skin_condition_question_answer_id