How to Parse Data in Json Format

How to parse data in JSON format

Very simple:

import json
data = json.loads('{"one" : "1", "two" : "2", "three" : "3"}')
print data['two'] # Or `print(data['two'])` in Python 3

How to parse JSON string in Typescript

Typescript is (a superset of) javascript, so you just use JSON.parse as you would in javascript:

let obj = JSON.parse(jsonString);

Only that in typescript you can have a type to the resulting object:

interface MyObj {
myString: string;
myNumber: number;
}

let obj: MyObj = JSON.parse('{ "myString": "string", "myNumber": 4 }');
console.log(obj.myString);
console.log(obj.myNumber);

(code in playground)

How to convert FormData (HTML5 object) to JSON

You could also use forEach on the FormData object directly:

var object = {};
formData.forEach(function(value, key){
object[key] = value;
});
var json = JSON.stringify(object);


UPDATE:

And for those who prefer the same solution with ES6 arrow functions:

var object = {};
formData.forEach((value, key) => object[key] = value);
var json = JSON.stringify(object);

UPDATE 2:

And for those who want support for multi select lists or other form elements with multiple values (since there are so many comments below the answer regarding this issue I will add a possible solution):

var object = {};
formData.forEach((value, key) => {
// Reflect.has in favor of: object.hasOwnProperty(key)
if(!Reflect.has(object, key)){
object[key] = value;
return;
}
if(!Array.isArray(object[key])){
object[key] = [object[key]];
}
object[key].push(value);
});
var json = JSON.stringify(object);

Here a Fiddle demonstrating the use of this method with a simple multi select list.

UPDATE 3:

As a side note for those ending up here, in case the purpose of converting the form data to json is to send it through a XML HTTP request to a server you can send the FormData object directly without converting it. As simple as this:

var request = new XMLHttpRequest();
request.open("POST", "http://example.com/submitform.php");
request.send(formData);

See also Using FormData Objects on MDN for reference:

UPDATE 4:

As mentioned in one of the comments below my answer the JSON stringify method won't work out of the box for all types of objects. For more information on what types are supported I would like to refer to the Description section in the MDN documentation of JSON.stringify.

In the description is also mentioned that:

If the value has a toJSON() method, it's responsible to define what data will be serialized.

This means that you can supply your own toJSON serialization method with logic for serializing your custom objects. Like that you can quickly and easily build serialization support for more complex object trees.

How to parse JSON format date string into date format

If there is no issue in adding a dependency, then you can add moment.js and it will help you to format data in any format
I am supposing that date from server is in this format '/Date(725828400000)/'

var d = item.EMP_DOB;
result.push(moment(Number(d.match(/\d+/)[0])).format('MM/DD/YYYY'));

If you are unable to add moment js then you can do soemthing like

var date = new Date(Number(d.match(/\d+/)[0]));
var day = date.getDate();
day = day = (day < 10) ? ("0" + day) : day;
var month = date.getMonth() + 1);
month = (month < 10) ? ("0" + month) : month;
var dateStr = day + "-" + month + "-" + date.getFullYear();
result.push(dateStr);

Parse data from JSON in ReactJS

React lives in JavaScript. So parsing a JSON string is done with:

var myObject = JSON.parse(myjsonstring);

How to fetch a file from somewhere with AJAX is a different question.

You could use fetch() for this. See for example

https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API or

https://davidwalsh.name/fetch or

https://blog.gospodarets.com/fetch_in_action

How to parse JSON to receive a Date object in JavaScript?

There is no standard JSON representation of dates. You should do what @jAndy suggested and not serialize a DateTime at all; just send an RFC 1123 date string ToString("r") or a seconds-from-Unix-epoch number, or something else that you can use in the JavaScript to construct a Date.



Related Topics



Leave a reply



Submit