0

I have an object for example in the form:

{
    "test": {
        "valid": false,
        "children": {
            "a": {
                "valid": false,
                "children": {
                    "1": {
                        "valid": false,
                        "children": {
                            "c": {
                                "valid": false
                            },
                            "d": {
                                "valid": false
                            }
                        }
                    }
                    ...
                }
            },
            "b": {
                "valid": false
            },
            // ...
        }
    }
}

I also have an input named name="test[a][1][c]" or name="test[b]". I need to get the get the valid property from the object with the same name of the input. Unfortunately I have no idea how to solve this problem.

My first problem is that I do not know how to divide the input name to a tree. Thank you for help.

edit:

How get universal value 'valid' from the name of INPUT?

I need from the name of INPUT create: json ['test']['children']['a']['children']['1']['children']['c']['valid']

Honza Synek
  • 125
  • 1
  • 8
  • 1
    Possible duplicate of [Access / process (nested) objects, arrays or JSON](http://stackoverflow.com/questions/11922383/access-process-nested-objects-arrays-or-json) – Teemu Jun 30 '16 at 15:45
  • You are missing a layer. You have object>test>children>a. You missed the children layer. – nurdyguy Jun 30 '16 at 15:46
  • Please demonstration when I have: var name = $ (this).attr('name') that is: test[a][1][c] and JSON tree is var tree. How do I get the value valid? – Honza Synek Jun 30 '16 at 15:54

1 Answers1

0

Use JSON.parse which deserializes JSON to produce a JavaScript object or array.

This example uses JSON.parse to deserialize JSON into the json_obj object.

var json_obj = JSON.parse(json);
var isValidForC = json_obj.test.children.a.children.1.children.c.valid;
var isValidForB = json_obj.test.children.b.valid;
Clive Seebregts
  • 2,004
  • 14
  • 18