-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);
    }
2

1 Answer 1

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.
Sign up to request clarification or add additional context in comments.

6 Comments

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
Please show the exact code you're using in your question, including the declaration of your some_json_object variable.
I received that JSON object as a response of a jquery AJAX
That's fine. So show the code that you're using to handle that response.
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?
|

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.