0

Like How to get JSON objects value if its name contains dots?, how do I use JSON.parse() function to make it an object?

For a JSON:

const mydata = {
    "key1" : "value1",
    "key2.part2": "value2" 
};

How should I define the class?

class MyData {
   key1: string;
   key2.part2: string;
}

key2.part2 would be invalid in a class.

csujiabin
  • 81
  • 1
  • 10
  • 1
    There's no JSON in your question so you already have your answer -> `"key2.part2"` – Andreas Oct 13 '21 at 06:44
  • You should use `interface`, not `class`. A `class` in JavaScript and TypeScript defines a constructor (and optionally: function properties), whereas JSON objects only have data properties, not function properties). – Dai Oct 13 '21 at 06:45
  • _"how do I use JSON.parse() function to make it an object?"_ - Keys in an object are always strings, hence `JSON.parse()` will just return that: `"key2.part2"` – Andreas Oct 13 '21 at 06:45

0 Answers0