Javascript how to parse JSON array
Javascript has a built in JSON parse for strings, which I think is what you have:
var myObject = JSON.parse("my json string");
to use this with your example would be:
var jsonData = JSON.parse(myMessage);
for (var i = 0; i < jsonData.counters.length; i++) {
var counter = jsonData.counters[i];
console.log(counter.counter_name);
}
Here is a working example
EDIT: There is a mistake in your use of for loop (I missed this on my first read, credit to @Evert for the spot). using a for-in loop will set the var to be the property name of the current loop, not the actual data. See my updated loop above for correct usage
IMPORTANT: the JSON.parse
method wont work in old old browsers - so if you plan to make your website available through some sort of time bending internet connection, this could be a problem! If you really are interested though, here is a support chart (which ticks all my boxes).
Convert array to JSON
Script for backward-compatibility:
https://github.com/douglascrockford/JSON-js/blob/master/json2.js
And call:
var myJsonString = JSON.stringify(yourArray);
Note: The JSON object is now part of most modern web browsers (IE 8 & above). See caniuse for full listing. Credit goes to: @Spudley for his comment below
Parse Json array in JavaScript
array.forEach(item => console.log(item.Name))
array.forEach(item => console.log(item.Status.Id))
Parsing JSON under Array object
That's not a JSON string, it's a JavaScript array. To make it a JSON string, surround it with apostrophes, then you can parse it, and finally loop through it:
var outObjA = '[{"LoginTime":"2018-02-14 08:51:48.0","User":"f00dl3","RemoteIP":"127.0.0.1"}]';
var outObjA = JSON.parse(outObjA);for (var i = 0; i < outObjA.length; i++) { var jsonData = outObjA[i]; console.log(jsonData);}
How to parse json array which contains multiple data types?
For this specific JSON format you can use the following:
Use Any as data type:
data class ElementsResponse(@SerializedName("elements") val elements: ArrayList<Any>)
And to consume the response, check if Any is String
or LinkedTreeMap
when(element) {
is String -> {
// task. or weeks.
println("string: $element")
}
is LinkedTreeMap<*, *> -> {
// key: image_url / value: https://www.graph-2x.jpg
println("image_url: ${element["image_url"]}")
}
}
Parse JSON Array Response
I'm not sure if the response you're getting is a string or an object.
Here's a fiddle that considers both scenarios and logs your expected output to the console.
https://jsfiddle.net/6yu9ngf5/2/
I've used JSON.parse(<string>)
for the case where the response is string.
For other case I just added data key to your response.
Parse json array using javascript
for(i=0;i<students.apResults.length;i++)
{
var contact = JSON.parse(students.apResults[i].offid);
alert(contact)
}
How to parse only specific attributes from a JSON file to an array
You can use Array.map()
function to remove the unnecessary fields like following example:
let res = {
"data": [
{
"id": 6,
"pnome": "dasda",
"unome": "dad",
"avatar": 1,
"email": "d",
"pass": "password",
"ponto": 0
},
{
"id": 3,
"pnome": "Enguias",
"unome": "Enguias outra vez",
"avatar": 10,
"email": "enguias@enguias.enguias",
"pass": "enguias",
"ponto": 0
},
{
"id": 2,
"pnome": "André",
"unome": "Marques",
"avatar": 1,
"email": "aglmarque@gmail.com",
"pass": "yet1",
"ponto": 0
}
]
}
res.data = res.data.map(item => {
return {
email: item.email,
pass: item.pass
};
});
console.log(res);
Parse JSON array in JavaScript into several Variables
You JSON is an array (since it's contained in [
and ]
), so you need:
var data = JSON.parse('[{"addre....}]');
var address = data[0].address,
postcode = data[0].postcode;
and so on...
Related Topics
Doesn't JavaScript Support Closures with Local Variables
Redirect a State to Default Substate with Ui-Router in Angularjs
Three.Js Generate Uv Coordinate
Removing an Anonymous Event Listener
Referencing "This" Inside Setinterval/Settimeout Within Object Prototype Methods
What Is Returned from a Constructor
How to Call Loading Function with React Useeffect Only Once
Rails - Could Not Find a JavaScript Runtime
Variable Scope in D3 JavaScript
Chrome Extension - Sendresponse Not Waiting for Async Function
Multiple Inheritance/Prototypes in JavaScript