C
C
cb77772020-08-18 16:02:10
JavaScript
cb7777, 2020-08-18 16:02:10

How to parse JSON into objects?

[
  {
    "street": {
      "id": 0,
      "name": "string",
      "type": {
        "id": 0,
        "name": "string"
      },
      "city": {
        "id": 0,
        "name": "string",
        "type": {
          "id": 0,
          "name": "string"
        }
      }
    },
    "house": "string",
    "id": 0
  }
]

There is json containing nested lists, how to parse it into separate objects?
on objects city, street, type
did through JSON.parse
getLocalStorage(key: string) {
    const myData = this.localStg.get(key);
    this.house = JSON.parse(myData);
  }

gives ERROR SyntaxError: Unexpected token o in JSON at position 1

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander, 2020-08-18
@Seasle

What is there localStg? And the JSON itself is perfectly parsed.

const json = `[
  {
    "street": {
      "id": 0,
      "name": "string",
      "type": {
        "id": 0,
        "name": "string"
      },
      "city": {
        "id": 0,
        "name": "string",
        "type": {
          "id": 0,
          "name": "string"
        }
      }
    },
    "house": "string",
    "id": 0
  }
]`;

JSON.parse(json);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question