3

I am having a json string

[{"part_id":"66","part_name":"Palet crate","part_image":"crate-box.gif","0":{"language_data_content":"Length [mm]","pgd_value":"1000"},"1":{"language_data_content":"Width [mm]","pgd_value":"800"},"2":{"language_data_content":"Height [mm]","pgd_value":"800"},"3":{"language_data_content":"Thickness [mm]","pgd_value":"20"}}]

This is a part of a Ajax response from my action in a controller

 for($i=0;$i<count($partlist);$i++){ 
 $jsondata[$i]['part_id'] = $partlist[$i]['part_id'];
 $jsondata[$i]['part_name'] = $partlist[$i]['part_name'];
 $jsondata[$i]['part_image'] = $partlist[$i]['part_image'];
 $gdata = $pgdata->getPropertyDimensions($partlist[$i]['part_id'],1);
 if(count($gdata) > 0){
 $j = 0;
 foreach($gdata as $g){
 $jsondata[$i][$j]['language_data_content'] = $g->language_data_content;
 $jsondata[$i][$j]['pgd_value'] = $g->pgd_value;
 $j++;
 }
 } 
 } 
 echo json_encode($jsondata);exit;

For one part id there can be multiple pgd_value In this json array there is only one part id and four pgd_value..on my ajax success function in looping this json as

success:function(msg){
str = '';
 if(msg.length > 0){
 for(j=0;j<msg.length;j++){
 str +='<span>'+msg[j]['part_id']+'</span>';
 // here i want to loop those four **pgd_value** values from json is it possible ?
}
}
}
asked Jun 15, 2012 at 4:37
1
  • 1
    What's the problem with this code? Commented Jun 15, 2012 at 4:41

4 Answers 4

2
var msg = [{
 "part_id": "66",
 "part_name": "Palet crate",
 "part_image": "crate-box.gif",
 "0": {
 "language_data_content": "Length [mm]",
 "pgd_value": "1000"
 },
 "1": {
 "language_data_content": "Width [mm]",
 "pgd_value": "800"
 },
 "2": {
 "language_data_content": "Height [mm]",
 "pgd_value": "800"
 },
 "3": {
 "language_data_content": "Thickness [mm]",
 "pgd_value": "20"
 }}];
 var data = msg[0]; // extract the whole object from array
 // loop over object
 for (var key in data) {
 // checking for key which contain pgd_value
 // as you mentioned about only 4 pgd_values 
 // so here is the checking for just 4 index
 if (key == '0' || key == '1' || key == '2' || key == '3') {
 // here I used square bracket notation to 
 // retrieve data from object
 alert(data[key].pgd_value);
 }
 }

DEMO

answered Jun 15, 2012 at 4:47
Sign up to request clarification or add additional context in comments.

Comments

1

You can use jQuery.map to select the pgd_values. See this fiddle: http://jsfiddle.net/HackedByChinese/6LLVe/1/

var str = '';
if (msg.length > 0) {
 for (j = 0; j < msg.length; j++) {
 var pgdValues = $.map(msg[j], function(item) {
 if (item['pgd_value']) return item['pgd_value'];
 });
 str += '<span> Part ID ' + msg[j]['part_id'] + ' pgd_values ' + pgdValues.join(', ') + '</span>';
 // OP: here i want to loop those four **pgd_value** values from json is it possible ?
 // ME: yep. all the IDs are now accessible from `pgdValues`
 }
}
answered Jun 15, 2012 at 4:49

Comments

1

You really should look into just deserializing the JSON into an actual object. It's a lot easier to work with and it makes your code much more readable.

See here: Deserializing from JSON into PHP, with casting?

answered Jun 15, 2012 at 4:42

Comments

1

This should work.

$.each(msg,function(index,item){
 alert("Part_ID : "+item.part_id);
 alert(item[0].pgd_value)
 alert(item[1].pgd_value)
 alert(item[2].pgd_value)
 alert(item[3].pgd_value)
})

Sample http://jsfiddle.net/Mc2du/29/

But if possible, try to change your JSON to send some string expression instead of 0/1. So that reading it is easy as well as robust). reading on an index is not safe as adding new content to the JSON structure would break the reading code.

Assuming you relace the 0/1 approach with string expressions like this (replaced 0 with Item0). You can read it like this

 $.each(msg,function(index,item){
 alert("Part_ID : "+item.part_id);
 alert(item.Item1.pgd_value)
 alert(item.Item2.pgd_value)
 alert(item.Item3.pgd_value)
 })

Sample : http://jsfiddle.net/Mc2du/28/

answered Jun 15, 2012 at 4:58

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.