Hello,
am an android developer, trying to learn Kony. I am using one open api. i dont know how to parse the json data in js as am completly new to js . what is the main difference between stringify and parse
my response : { "status": 1, "count": 2, "posts": [ { "days": { "details": [ { "place": "labs", "StartTime": "01:00:00", "EndTime": "02:00:00", "Description": "Meeting with team", "participants": [ { "Name": "KK", "Designation": "VP, Operations", "ContactNumber": "111" }, { "Name": "MN1", "Designation": "Project Lead", "ContactNumber": "111" } ] } ], "Date": [ "2017-02-02" ] }, "name": "test" }, { "days": { "details": [ { "place": "India", "StartTime": "01:00:00", "EndTime": "03:00:00", "Description": "Agenda1", "participants": [ { "Name": "Kk", "Designation": "VP, Operations", "ContactNumber": "11111" }, { "Name": "MN", "Designation": "Project Lead", "ContactNumber": "111" } ] }, { "place": "microsoft", "StartTime": "01:00:00", "EndTime": "02:00:00", "Description": "Meet CEO", "participants": [ { "Name": "VR", "Designation": "Project Lead", "ContactNumber": "111" } ] }, { "place": "microsoft", "StartTime": "01:00:00", "EndTime": "02:00:00", "Description": "Meet CEO", "participants": [ { "Name": " VR", "Designation": "Project Lead", "ContactNumber": "111" } ] }, { "place": "Formule", "StartTime": "10:50:00", "EndTime": "11:50:00", "Description": "Meet Rajesh", "participants": [ { "Name": "MN", "Designation": "Project Lead", "ContactNumber": "111" } ] }, { "place": "Dell", "StartTime": "04:00:00", "EndTime": "08:00:00", "Description": "Agenda 2", "participants": [ { "Name": "MN", "Designation": "Project Lead", "ContactNumber": "1111111" } ] } ], "Date": [ "2017-02-03" ] }, "name": "test" } ] }
or using the fetch api and Babel you can:
res = await fetch('/posts')
posts = await res.json()
where posts is an object parsed from the response json
see also: developer.mozilla.org/en-US/docs/Web/API/Fetch_AP…
Agree with @pdavis. To expound a little:
If you're using AJAX and the content type you're sending back is JSON, you don't need to do anything. Assign the response to a variable and use as a normal object. (e.g. response.participants.Designation)
If your content type is not JSON, then you'd use something like this to turn it into JSON: var response = JSON.parse(myResponse);
Then, you could use it like a normal object (e.g. response.partipants.Designation).
If you're looking to display the value of a JSON object's property, that's where stringify can help. For example: console.log(JSON.stringify(myResponse)).
That said, Chrome can now display a JSON object in the console without a problem. This didn't used to be the case. It used to be you needed to use JSON.stringify. Now, you don't, and it will display the JSON object as an expandable tree.
Also, little trick... use jsonlint.com to paste your JSON in to make sure it validates okay. It's a great tool.
JSON.parse takes a string like you've got there and deserializes it into objects that your JavaScript can operate on. JSON.stringify is the inverse of that... it takes a JavaScript object and serializes it into a string (hence the name "stringify"... turn into a string)
TheSheriff
Co-Founder, Founder, Entrepreneur & Problem Solver
stringify= turn a JSON (an Object) into a stringparse= The opposite ofstringify, turns a string into an Object.Example:
myresponse = [{"status": 1, "count": 2, }]; var stringy = JSON.stringify(myresponse); var parsed = JSON.parse(stringy);