0

Have problem with converting string to object. I'm getting from the server array of object and some of these objects contains other objects. Problem is that JS reads inner object like a string.

Data structure:

[
{
    name: String("Name1"),
    key: String("name1"),
    value: String("some text")
},
{
    name: String("Name2"),
    key: String("name2"),
    value: [
        {
            name: String("Object1"),
            key: String("object1"),
            value: [    
                {
                    name: String("Object1 Name3"),
                    key: String("object1.name3"),
                    value: Object()
                }
            ]
        },
        {
            name: String("Name4"),
            key: String("name4"),
            value: String()
        },
    ]
},
{
    name: String("Name5"),
    key: String("name5"),
    value: Number("44")
}
]

Object with Name2 contains other other object like value.

Problem that when I'm trying to display this object tree with Angular object with name ** Object1 Name3** is a string.

How I can convert all data that I'm getting from the server to OBJECT?

This object tree can have up to 5 nested objects and check value of each of them not a solution for me.

Lugaru
  • 1,430
  • 3
  • 25
  • 38
  • You've done something wrong there: `{[]}`. You can't have an array like that, JS objects are key:value pairs. Remove the curly braces `{}` from all the `value:` values. NB the prototype of strings built with `new String()` is `Object`, not `String` – webketje Jun 15 '14 at 12:58
  • That was misspelled only here, data structure is correct. – Lugaru Jun 15 '14 at 13:01
  • 2
    + Yeah you can't expect a JSON obj to be something else than a `String`. You need to parse it before with [`JSON.parse(myJSONfile)`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse) – webketje Jun 15 '14 at 13:03
  • @Tyblitz, yep, but the problem with that solution that it converts only top level object but if I have an object like value of other object it still string. – Lugaru Jun 15 '14 at 13:08
  • That's not true, I've done it successfully before. Check this out: http://stackoverflow.com/questions/722668/traverse-all-the-nodes-of-a-json-object-tree-with-javascript – webketje Jun 15 '14 at 13:20
  • Get rid of all the `String("foo")` and `Object()` stuff and just use primitive/literal strings and objects as values. Then use `JSON.stringify(myfoo)` and `JSON.parse('jsonStructure')`. You lose more than you're trying to gain by trying to use those value types in that json structure. So - instead of `name: String("name1")`, just do `name: "name1"` -- or instead of `value: Object()`, just do `value: {}`. – J.Wells Jun 15 '14 at 13:48
  • 4
    Don't use `JSON.stringify` and `JSON.parse` to work with jsons in angular. Use `angular.toJson` and `angular.fromJson`instead. `angular.toJson` escapes unnecesary `$$` characters. – akn Jun 15 '14 at 14:04
  • 2
    @czwek: It doesn't escape characters - it ignored properties whose name starts with `$$`. – gkalpak Jun 15 '14 at 15:47

0 Answers0