This isn't a single JSON object. You have an array of JSON objects. You need to loop over array first and then access each object. Maybe the following kickoff example is helpful:
var arrayOfObjects = [{
"id": 28,
"Title": "Sweden"
}, {
"id": 56,
"Title": "USA"
}, {
"id": 89,
"Title": "England"
}];
for (var i = 0; i < arrayOfObjects.length; i++) {
var object = arrayOfObjects[i];
for (var property in object) {
alert('item ' + i + ': ' + property + '=' + object[property]);
}
// If property names are known beforehand, you can also just do e.g.
// alert(object.id + ',' + object.Title);
}
If the array of JSON objects is actually passed in as a plain vanilla string, then you would indeed need eval() here.
var string = '[{"id":28,"Title":"Sweden"}, {"id":56,"Title":"USA"}, {"id":89,"Title":"England"}]';
var arrayOfObjects = eval(string);
// ...
To learn more about JSON, check MDN web docs: Working with JSON .
Answer from BalusC on Stack OverflowThis isn't a single JSON object. You have an array of JSON objects. You need to loop over array first and then access each object. Maybe the following kickoff example is helpful:
var arrayOfObjects = [{
"id": 28,
"Title": "Sweden"
}, {
"id": 56,
"Title": "USA"
}, {
"id": 89,
"Title": "England"
}];
for (var i = 0; i < arrayOfObjects.length; i++) {
var object = arrayOfObjects[i];
for (var property in object) {
alert('item ' + i + ': ' + property + '=' + object[property]);
}
// If property names are known beforehand, you can also just do e.g.
// alert(object.id + ',' + object.Title);
}
If the array of JSON objects is actually passed in as a plain vanilla string, then you would indeed need eval() here.
var string = '[{"id":28,"Title":"Sweden"}, {"id":56,"Title":"USA"}, {"id":89,"Title":"England"}]';
var arrayOfObjects = eval(string);
// ...
To learn more about JSON, check MDN web docs: Working with JSON .
This is your dataArray:
[
{
"id":28,
"Title":"Sweden"
},
{
"id":56,
"Title":"USA"
},
{
"id":89,
"Title":"England"
}
]
Then parseJson can be used:
$(jQuery.parseJSON(JSON.stringify(dataArray))).each(function() {
var ID = this.id;
var TITLE = this.Title;
});
Create JSON with Arrays of different types?
api design - Representing a large list of complex objects in JSON - Software Engineering Stack Exchange
Convert JSON file to Typescript Array of Objects
The interface has an iD field and the json has a date field. They need to be the same types. You also might need to use JSON.parse(stringValue) depending on how you are getting the JSON data.
More on reddit.comHow to access nested array of objects from json server?
I’m not sure I understand your problem correctly.. but from what I understand, you need to change your server to return the values you want for a given route (url). For instance you could have a route like /starttime and your server handles this route by returning only the start time.
Otherwise, you need to handle this on the frontend. You make your call, get the JSON data. And then pull out starttime from the data:
-
You fetch
-
You parse the JSON
-
You access the starttime value
Edit: look into the .find() function in javascript. That’s what you’ll need if you can’t modify the backend
Edit2: DM if you need more help :)
More on reddit.comVideos
I am trying to make a JSON file using C#, and to make a simple one seems pretty easy. but on thing I am struggling with is an array. The entire file falls inside an array, and I can't seem to find documentation enough to make that happen though. Does anyone know how to do this?
Below I posted a link to an example of a JSON I would be interested in making. I created an object in C# that has every variable listed, but I don't know how to format it to have every variable listed in the correct spot. I appreciate any help. Thank you!
https://help.viewpoint.com/en/spectrum/spectrum/api-web-services/api-web-services/list-of-web-services/accounts-payable-services/vendor-invoice-multi-line
Edit: Does indentation matter in JSON? I suppose I could just hardcore a massive string and insert the variables as needed if that’s that case
Lean towards option 1, as it's a more expected format.
Option 1 works with JSON as it's designed to be used and therefore benefits from what JSON offers (a degree of human readability, which is good for debugging, and straightforward parsing, which is good for limiting entire categories of bugs to begin with).
Option 2 begrudgingly adopts JSON and subverts many of the benefits. If you don't want human readability, use protobuf or something similar... AIWalker's "CSV"-like approach isn't terrible either. It is marginally better (readable) than splitting objects apart and recombining them. But, this is still not as good (readable) as using JSON "as designed".
Also bear in mind, your API responses are also likely going to be gzipped. Most of the repetition in option 1 will be quickly and transparently condensed over the wire.
As an aside, if you're moving a lot of data, also consider JSONL or paginated results. Pagination can be especially helpful for web clients, as it places natural pauses in the processing, providing a degree of "organic" protection against UI lockups.
A list of objects is easier to work with. You can use append, map, filter... All the nice things JS Arrays have which manual indexing doesn't. And there's no way to get out of sync, so that's an entire class of bugs gone.
If you're worried about efficiency:
- Measure (premature optimization is the root of all evil)
- Consider the list of lists trick AIWalker proposed
- Consider an outright binary format
- Make sure gzip is enabled
- Measure (it's worth saying twice)