-2

I have this JSON object and I want to navigate trought it with javascript or jquery, how can I do that?

{
    "Fporder": {
        "cep_id": "300000007",
        "user_id": "300000192",
        "fporder_type_id": 2,
        "agency_id": null,
        "fporder_code": 1,
        "id": "66"
    },
    "FpordersProduct": [
        {
            "product_id": "1938",
            "requested": "4",
            "price": 3965,
            "product_name": "PLANCHA DE LASAGNA PRE COCIDO X KL",
            "product_code": "22044001"
        }
    ]
}

EDIT:

Please I'm getting this error:

Uncaught TypeError: Cannot read property 'product_id' of undefined

I have tried these with no success:

json_object.FpordersProduct.product_id

OR

json_object.FpordersProduct[0].product_id

OR

json_object.FpordersProduct[0]['product_id']

But it does not work. Please Help.

EDIT2

function agregar_fporder(fporder)
    {
        console.log('agregar_fporder() ejecutado');
        console.log("fporder: "+fporder);
        var tr = '';
        tr += '<tr class="item fporder">';
        tr +=       '<td class="nro_orden"></td>';
        tr +=       '<td class="id">'+fporder.FpordersProduct.product_id+'</td>';
        tr +=       '<td class="code">'+fporder.FpordersProduct.product_code+'</td>';
        tr +=       '<td class="name">'+fporder.FpordersProduct.product_name+'</td>';
        tr +=       '<td></td>';
        tr +=       '<td></td>';
        tr +=       '<td></td>';
        tr +=       '<td></td>';
        tr +=       '<td></td>';
        tr +=       '<td class="nro_pedido">';
        tr +=       '</td>';
        tr +=       '<td class="quantity">';
        tr +=       '</td>';
        tr +=       '<td class="um_id">';
        tr +=       '</td>';
        tr +=       '<td></td>';
        tr +=       '<td>';
        tr +=       '</td>';
        tr +=       '<td></td>';
        tr +=       '<td></td>';
        tr += '</tr>';

        var item = $('table#items tr.items.fporder').find('td.code input.code[value="'+fporder.FpordersProduct.product_code+'"');
        console.log('item: '+item);
    }
juanpscotto
  • 990
  • 1
  • 13
  • 32

1 Answers1

0

Well, "navigate through it" is very obscure. However, it's an object so if all you need to do is access it's properties, you can do so like any other object.

Examples:

var some_json_object = {
  "Fporder": {
    "cep_id": "300000007",
    "user_id": "300000192",
    "fporder_type_id": 2,
    "agency_id": null,
    "fporder_code": 1,
    "id": "66"
  },
  "FpordersProduct": [
    {
      "product_id": "1938",
      "requested": "4",
      "price": 3965,
      "product_name": "PLANCHA DE LASAGNA PRE COCIDO X KL",
      "product_code": "22044001"
    }
  ]
}

some_json_object.Fporder // to access Fporder property on root of object

some_json_object.Fporder.cep_id // to access cep_id property on the object assigned to the Fporder property

some_json_object.FpordersProduct[0] // to access the first (currently only) object on the array assigned to the FpordersProduct property

// ... etc.
jeffdill2
  • 3,968
  • 2
  • 30
  • 47
  • And How I access through the first element of some_json_object.FpordersProduct[0], because I'm getting and error doing like this some_json_object.FpordersProduct[0].product_id – juanpscotto Jan 21 '16 at 19:33
  • Please show the exact code you're using in your question, including the declaration of your `some_json_object` variable. – jeffdill2 Jan 21 '16 at 20:23
  • I received that JSON object as a response of a jquery AJAX – juanpscotto Jan 21 '16 at 20:26
  • That's fine. So show the code that you're using to handle that response. – jeffdill2 Jan 21 '16 at 20:28
  • The piece of code you just added is referencing the `fporder` parameter. You said when you tried `json_object.FpordersProduct.product_id` it didn't work. Where is `json_object` defined? – jeffdill2 Jan 21 '16 at 21:08
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/101343/discussion-between-jeffdill2-and-juanpscotto). – jeffdill2 Jan 21 '16 at 21:18