Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to map JSON data to a class

Tags:

People also ask

Can you map a JSON?

You can't. The keys of a map can be anything, including objects. But JSON syntax only allows strings as keys. So it's impossible in a general case.

What is dynamic JSON?

A dynamic JSON file will be created to store the array of JSON objects. Consider, we have a database named gfg, a table named userdata. Now, here is the PHP code to fetch data from database and store them into JSON file named gfgfuserdetails. json by converting them into an array of JSON objects.


I created a ES6 class by Babel and I want to map JSON data which is gotten from a server to the ES6 class.
Is there anything common way to do that?

User.js

export default class User {   constructor() {     this.firstName;     this.lastName;     this.sex;   } } 

app.js

import User from "./classes/User";  var data = JSON.parse(req.responseText); console.log(data.firstname); //Bob //now...just set data one by one?