var arr = [ {"id":"10", "class": "child-of-9"}, {"id":"11", "class": "child-of-10"}];
for (var i = 0; i < arr.length; i++){
document.write("<br><br>array index: " + i);
var obj = arr[i];
for (var key in obj){
var value = obj[key];
document.write("<br> - " + key + ": " + value);
}
}
note: the for-in method is cool for simple objects. Not very smart to use with DOM object.
Answer from Your Friend Ken on Stack Overflowvar arr = [ {"id":"10", "class": "child-of-9"}, {"id":"11", "class": "child-of-10"}];
for (var i = 0; i < arr.length; i++){
document.write("<br><br>array index: " + i);
var obj = arr[i];
for (var key in obj){
var value = obj[key];
document.write("<br> - " + key + ": " + value);
}
}
note: the for-in method is cool for simple objects. Not very smart to use with DOM object.
Taken from jQuery docs:
var arr = [ "one", "two", "three", "four", "five" ];
var obj = { one:1, two:2, three:3, four:4, five:5 };
jQuery.each(arr, function() {
$("#" + this).text("My id is " + this + ".");
return (this != "four"); // will stop running to skip "five"
});
jQuery.each(obj, function(i, val) {
$("#" + i).append(document.createTextNode(" - " + val));
});
How to loop through each item of a json array?
How to loop through an array with JSON objects
Use of .Object in this forEach loop example
Iterate through json with javascript (forEach)
Videos
The names are a little confusing because of O and 0 ! But you are almost there, here is the fixed code which at least parses your sample json data correctly:
public class MyModel
{
public Outputdata Outputdata { get; set; }
}
public class Outputdata
{
public TASK_0 TASK_0 { get; set; }
public string ERROR_O { get; set; }
}
public class TASK_0
{
public List TASK_O_ITEM { get; set; }
}
public class TASK_O_ITEM
{
public string CODE { get; set; }
public string TASK_NAME { get; set; }
}
And the usage:
var responseBody = "the body from wherever you read";
var result = JsonConvert.DeserializeObject(responseBody);
foreach (var item in result?.Outputdata?.TASK_0?.TASK_O_ITEM)
Console.WriteLine($"{item.CODE} - {item.TASK_NAME}");
Hi @Paul
My thoughts are:
- Declare classes and lists.
- Assign a value to the data.
- Generate JSON variables.
- Print it out
For demonstration purposes, I printed the JSON data as string data.
In order to keep the same format as JSON, I set an option.
var options = new JsonSerializerOptions { WriteIndented = true };
string jsonString = System.Text.Json.JsonSerializer.Serialize(outputdata, options);
Of course, you can also print it directly.
You can refer to the following code:
public class TASK_O
{
public List TASK_O_ITEM { get; set; }
}
public class Outputdata
{
public TASK_O TASK_O { get; set; }
public string ERROR_O { get; set; }
}
public class Example
{
public Outputdata Outputdata { get; set; }
}
public class TASK_O_ITEM
{
public string CODE { get; set; }
public string NAME { get; set; }
}
internal class Program
{
static void Main(string[] args)
{
var task_o_item = new List {
new TASK_O_ITEM
{
CODE = "123",
NAME = "ABC"
},
new TASK_O_ITEM
{
CODE = "456",
NAME = "DEF"
},
new TASK_O_ITEM
{
CODE = "789",
NAME = "GHI"
},
};
var task_O = new TASK_O
{
TASK_O_ITEM = task_o_item
};
var outputdata = new Outputdata
{
TASK_O = task_O,
ERROR_O = null
};
var example = new Example
{
Outputdata=outputdata
};
var options = new JsonSerializerOptions { WriteIndented = true };
string jsonString = JsonSerializer.Serialize(example, options);
Console.WriteLine(jsonString);
Console.Read();
}
}
Result:
Best Regards
Qi You
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
Hi all,
Im really struggling with this problem today. So basically, I have an array in the format
arr = [{title: " some title", id: "some id"}, {title: " some title2", id: "some id2"}] and all im trying to do is loop through each item in the array and get the value of the ids.
Here is what ive tried:
for( var i = 0; i< arr.length; i++){
console.log(arr[i].id)
}
It keeps showing up as undefined, please can anyone assist me? I would like the result to be "some id"
You can do something like
for(var k in result) {
console.log(k, result[k]);
}
which loops over all the keys in the returned json and prints the values. However, if you have a nested structure, you will need to use
typeof result[k] === "object"
to determine if you have to loop over the nested objects. Most APIs I have used, the developers know the structure of what is being returned, so this is unnecessary. However, I suppose it's possible that this expectation is not good for all cases.
Try this:
$.each(result,function(index, value){
console.log('My array has at position ' + index + ', this value: ' + value);
});