All current browsers have native JSON support built in. So as long as you're not dealing with prehistoric browsers like IE6/7 you can do it just as easily as that:
var j = {
"name": "binchen"
};
console.log(JSON.stringify(j));
Answer from Andris on Stack OverflowAll current browsers have native JSON support built in. So as long as you're not dealing with prehistoric browsers like IE6/7 you can do it just as easily as that:
var j = {
"name": "binchen"
};
console.log(JSON.stringify(j));
With JSON.stringify() found in json2.js or native in most modern browsers.
JSON.stringify(value, replacer, space) value any JavaScript value, usually an object or array. replacer an optional parameter that determines how object values are stringified for objects. It can be a function or an array of strings. space an optional parameter that specifies the indentation of nested structures. If it is omitted, the text will be packed without extra whitespace. If it is a number, it will specify the number of spaces to indent at each level. If it is a string (such as "\t" or " "), it contains the characters used to indent at each level.
Converting a .json to .js
Extension to convert js object to JSON?
How do I convert normal JSON to GeoJSON?
GeoJSON is just JSON, but with a specific schema that is understood by many GIS programs. Without knowing more about your particular data it's not easy to recommend a way to convert it.
Generically, learn the GeoJSON spec and enough Node.js (any programming language works, but JavaScript and JSON go together like peanut butter and jelly), to convert the data. It doesn't have to be pretty elegant code, just enough to get the job done.
More on reddit.comhow to dynamically map a json response object to an entity?
Don't just convert http request observables to promises. You will want to learn observables and the sooner the better.
As for converting the data there are two ways to look at it. You can either use the data as dtos and do any mutations on pure data or you can use actual objects. Either way there's nothing here that is angular specific it's just Javascript / typescript.
If you want to use dtos your done. You already said the return type was User by putting it inside the generic return field of the promise (Promise<User>).
If you want to use an actual User object you have defined somewhere then you just need to make a new one and return it instead of the json. It would look something like
response=>new User(response.json())
And the user constructor would be responsible for parsing the json into its own fields.
As for using observables instead of promises it would just look like
getUser(...) {
this.http.get(...).map(resp=>resp.json());
}And alternatively as above you could create a User object with
resp=>new User(resp.json())
Then wherever you want to call it you would do
getUser(...).subscribe(user=>
localUserVariable = user);Note that the next version of angular looks like the default is to call .json() on the response automatically so you won't have to do that anymore.
More on reddit.comVideos
JSON.parse(jsonString) is a pure JavaScript approach so long as you can guarantee a reasonably modern browser.
The jQuery method is now deprecated. Use this method instead:
let jsonObject = JSON.parse(jsonString);
Original answer using deprecated jQuery functionality:
If you're using jQuery just use:
jQuery.parseJSON( jsonString );
It's exactly what you're looking for (see the jQuery documentation).
Teaching my self how to do JSON with javascript. When I initally have data and the file is like user.json, should I just convert it into .js . What is the most common thing to do? I have been fetching the data and changing it into .js file
Right now I am teaching myself how to fetch local data and data from a API, grab some of it and display it on a html page. I am also a bit confuse whether everything starts with async function await and thens but it seems to be repetitive or at least the way I been doing it.