c# loop through an array to display like json code example
Example 1: loop over json javascript
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);
}
}
Example 2: c# foreach object in array json
public class ChatMessage
{
public string author;
public string content;
public int timestamp;
}
public class ChatResponse
{
public List<ChatMessage> chat;
public bool error;
public string message;
}
ChatResponse response = JsonConvert.DeserializeObject<ChatResponse>(json);
foreach (var message in response.chat)
{
rtbChat.AppendText($"{message.author}: {message.content}\n");
}