3

In the following code:

$.getJSON('<?php echo $this->Html->url(array('controller'=>'accounts', 'action'=>'get_customers_order_mcs')); ?>/'+customer_order_id,
                function(data){
                    var json_mc = data.MasterCarton.mc;
                    alert(json_mc);
                    $.each(json_mc,function(){
                         console.log(this);
                   });
             });

The data sent as response is as follows-

{
     "MasterCarton":{
                         "id":"40",
                         "mc":"[
                                    "1":{\"mc_number\":\"Warehouse1\\\/2013-2014\\\/CO\\\/ABC Corp\\\/239\\\/101-Red-1\",\"config\":{\"S2\":10,\"S1\":10},\"delivered\":0},            "2":{\"mc_number\":\"Warehouse1\\\/2013-2014\\\/CO\\\/ABC Corp\\\/239\\\/101-Red-2\",\"config\":{\"S2\":10,\"S1\":10},\"delivered\":0},                  "3":{\"mc_number\":\"Warehouse1\\\/2013-2014\\\/CO\\\/ABC Corp\\\/239\\\/104-Black-3\",\"config\":{\"S1\":7,\"S2\":7,\"S5\":6},\"delivered\":0},               "4":{\"mc_number\":\"Warehouse1\\\/2013-2014\\\/CO\\\/ABC Corp\\\/239\\\/104-Black-4\",\"config\":{\"S1\":7,\"S2\":7,\"S5\":6},\"delivered\":0},              "5":{\"mc_number\":\"Warehouse1\\\/2013-2014\\\/CO\\\/ABC Corp\\\/239\\\/104-Black-5\",\"config\":{\"S1\":6,\"S2\":6,\"S5\":7},\"delivered\":0}
                               ]",
                         "delivery_note_id":"0",
                         "customer_order_id":"314"
                 }
}

The json array inside of mc is as shown below-

[
   "1":{
      "mc_number":"Warehouse1\/2013-2014\/CO\/ABC Corp\/239\/101-Red-1",
      "config":{
         "S2":10,
         "S1":10
      },
      "delivered":0
   },
   "2":{
      "mc_number":"Warehouse1\/2013-2014\/CO\/ABC Corp\/239\/101-Red-2",
      "config":{
         "S2":10,
         "S1":10
      },
      "delivered":0
   },
   "3":{
      "mc_number":"Warehouse1\/2013-2014\/CO\/ABC Corp\/239\/104-Black-5",
      "config":{
         "S1":6,
         "S2":6,
         "S5":7
      },
      "delivered":0
   }
]

I am trying to parse each object in it using jquery, and each object is as follows-

    {
   "mc_number":"Warehouse1\/2013-2014\/CO\/ABC Corp\/239\/101-Red-1",
   "config":{
      "S2":10,
      "S1":10
   },
   "delivered":0
}

To get the above object I using the following jquery code-

$.each(json_mc,function(){
       // What should the code be so as to get each individual objects.         
});

That is each and every time of .each should get me-

   {
      "mc_number":"Warehouse1\/2013-2014\/CO\/ABC Corp\/239\/101-Red-1",
      "config":{
         "S2":10,
         "S1":10
      },
      "delivered":0
   }

1 Answer 1

7

What you're after is simply this which is set at every iteration:

$.each(json_mc,function(){
    console.log(this);
});

Update

Given the raw response you've shown, you may need to decode it one more time:

$.each($.parseJSON(json_mc), function() {
    console.log(this);
});
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for the answer jack but when i tried with your solution I am getting [, {, ", m, c ........... so on. So can you plese suggest me the way in which the whole object from { to } is obtained.
@GaneshYoganand It works fine here. The results you're getting could be due to code that you haven't shown.
I am getting the log like this. String { 0= "m" } add_sales_invoice (line 294) String { 0= "c" } add_sales_invoice (line 294) String { 0= "_" } add_sales_invoice (line 294) String { 0= "n" }
@GaneshYoganand You will have to share some of that code, I have no idea what you're doing with the data.
Jack please look at the question I have edited. I need each objects of mc. What can I do?

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.