jQuery - How to get JSON array name from nested JSON

user3000138

I'm a newbie in jQuery and JSON. I hope you can help me with my little task. I have the following JSON object structure:

{
    "recipe": {
        "myREAL":0,
        "mySTRING":"STRING variable",
        "myUDINT":0,
        "mybool":0,
        "ingNumber":[0,0,0,0,0],
        "ingMinimumWeight":[0,0,0,0,0],
        "ingNominalWeight":[0,0,0,0,0]
    }
}

I would like to get the name of the array name instead of only getting the index and the value of the index.

My output so far:

 Key: myREAL Value: 0
 Key: mySTRING Value: STRING variable
 Key: myUDINT Value: 0
 Key: mybool Value: 0
 Key: 0 Value: 0
 Key: 1 Value: 0
 Key: 2 Value: 0
 Key: 3 Value: 0
 Key: 4 Value: 0
 ...

I'm interested in such structure:

 Key: myREAL Value: 0
 Key: mySTRING Value: STRING variable
 Key: myUDINT Value: 0
 Key: mybool Value: 0
 ingNumber:
 Key: 0 Value: 0
 Key: 1 Value: 0
 Key: 2 Value: 0
 Key: 3 Value: 0
 Key: 4 Value: 0
 ingMinimumWeight:
 ...

I would like to do it in Jquery without specifiying the name of the array variable.

My jQuery code:

function iter(obj) {
    for (var key in obj) {
        if (typeof (obj[key]) == 'object') {
            iter(obj[key]);
        } else {
            content = " Key: " + key + " Value: " + obj[key] + '\n';
            var box = $("#myArea");
            box.val(box.val() + content);
        } 
    }
}
mohamedrias

As per your desired output, you need to add content to box in before recursively calling the function again.

function iter(obj) {
     var box = $("#myArea");
     for (var key in obj) {
        if (typeof (obj[key]) == 'object') {
            content = key+ " : \n";
            box.val(box.val() + content);
            iter(obj[key]);
        } else {
            content = " Key: " + key + " Value: " + obj[key] + '\n';
            box.val(box.val() + content);
        } 
    }
}

DEMO

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

how to get a particular object value from nested json array

分類Dev

get value from nested json array

分類Dev

How to get data from a json array without directly making reference to the array name

分類Dev

How can I get the key name and its value from an array within a JSON object

分類Dev

How to query deep nested json array from couchbase?

分類Dev

Get column name from JSON

分類Dev

How to get json value from json array using javascript

分類Dev

Android - How to get key name from JSON in addition to the value

分類Dev

How to get a name value from a foreign key of a Json object

分類Dev

Return Nested Json from database as an array with Laravel

分類Dev

Get Nested value from JSON response with python

分類Dev

Get nested data from local JSON file

分類Dev

How to display multiple array sourced from JSON using jQuery?

分類Dev

how to alert each values from json array in jquery

分類Dev

How to parse nested json array in android

分類Dev

How to get json return value from php by javaScript / jquery?

分類Dev

How do I get data from JSON Array MSSQL

分類Dev

How to parse JSON array from a GET request in react-native?

分類Dev

How to get the information from JSON array into list and into XAML

分類Dev

How get data from array in json item using angularjs

分類Dev

vue.js - recursive components doesn't get updated when using nested array from raw json

分類Dev

How to parse json dynamic array name

分類Dev

How to parse json array in android with no name?

分類Dev

How to take a value from nested array(json format) without using multiple foreach in php

分類Dev

How to get a json object with specific key values, from a json array column?

分類Dev

How to get **nested** objects as JSON from Ruby on the backend to AJAX in the front-end

分類Dev

How do i call the parsed data from the GET request that have a longer nested JSON structure in Swift?

分類Dev

Use jq to get array from json

分類Dev

Get JSON value from API array

Related 関連記事

  1. 1

    how to get a particular object value from nested json array

  2. 2

    get value from nested json array

  3. 3

    How to get data from a json array without directly making reference to the array name

  4. 4

    How can I get the key name and its value from an array within a JSON object

  5. 5

    How to query deep nested json array from couchbase?

  6. 6

    Get column name from JSON

  7. 7

    How to get json value from json array using javascript

  8. 8

    Android - How to get key name from JSON in addition to the value

  9. 9

    How to get a name value from a foreign key of a Json object

  10. 10

    Return Nested Json from database as an array with Laravel

  11. 11

    Get Nested value from JSON response with python

  12. 12

    Get nested data from local JSON file

  13. 13

    How to display multiple array sourced from JSON using jQuery?

  14. 14

    how to alert each values from json array in jquery

  15. 15

    How to parse nested json array in android

  16. 16

    How to get json return value from php by javaScript / jquery?

  17. 17

    How do I get data from JSON Array MSSQL

  18. 18

    How to parse JSON array from a GET request in react-native?

  19. 19

    How to get the information from JSON array into list and into XAML

  20. 20

    How get data from array in json item using angularjs

  21. 21

    vue.js - recursive components doesn't get updated when using nested array from raw json

  22. 22

    How to parse json dynamic array name

  23. 23

    How to parse json array in android with no name?

  24. 24

    How to take a value from nested array(json format) without using multiple foreach in php

  25. 25

    How to get a json object with specific key values, from a json array column?

  26. 26

    How to get **nested** objects as JSON from Ruby on the backend to AJAX in the front-end

  27. 27

    How do i call the parsed data from the GET request that have a longer nested JSON structure in Swift?

  28. 28

    Use jq to get array from json

  29. 29

    Get JSON value from API array

ホットタグ

アーカイブ