Reputation:
I would like to deserialize a JSON object like this:
[{"Response":"OK","UUID":"89172"},{"Response":"OK","UUID":"10304"}]
into a custom class where it has variables storing Response
and UUID
. However I would want to deserialize multiple data response such as above example. It will be great if I can use the method ForEach
such that I can pop the data out accordingly. Can anyone advise? Many Thanks!
Upvotes: 0
Views: 16300
Reputation:
I've finally resolved this problem thanks with the help of @Newton Sheikh. Thank you first of all.
First I created a class (Student
)
public class Student
{
public string Response { get; set; }
public string UUID { get; set; }
}
Then I imported the JSON.NET and created a function:
public List<Student> ReturnAllStudentsList()
{
string jsonString = "[{'Response':'OK','UUID':'89172'},{'Response':'OK','UUID':'10304'}]";
List<Student> Students = new List<Student>(); //Creates a list of custom Type: Student
var result = JsonConvert.DeserializeObject<List<Student>>(jsonString);
foreach (var student in result)
{
Students.Add(student);
}
return Students;
}
From this point, I have a list of Students. Then in my main program, I call this function:
private void button1_Click(object sender, EventArgs e)
{
List<Student> Students = ReturnAllStudentsList(); // Gets the list from JSON.
foreach(Student student in Students)
{
// Here I can access to each student for every loop cycle.
MessageBox.Show(student.Response);
}
}
Thank you @Newton Sheikh and others help! I hope this example code can help others too! Cheers.
Upvotes: 0
Reputation: 1396
write this class
public class MyClass
{
public string Response { get; set; }
public string UUID { get; set; }
}
then you can deserialize it using the library newtonsoft.json
string jsonString = "[{"Response":"OK","UUID":"89172"},{"Response":"OK","UUID":"10304"}]";
...
...
var myListOfItems= JsonConvert.DeserializeObject<List<MyClass>>(jsonString);
foreach(var item in myListOfItems)
{
....
}
FULL CODE IN CONSOLE APPLICATION
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Newtonsoft.Json;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string jsonString = "[{'Response':'OK','UUID':'89172'},{'Response':'OK','UUID':'10304'}]";
var items= JsonConvert.DeserializeObject<List<MyClass>>(jsonString);
foreach (var item in items)
{
Console.WriteLine("UUUID: "+item.UUID);
Console.WriteLine("Response: " + item.Response);
Console.WriteLine();
}
Console.ReadKey();
}
}
public class MyClass
{
public string Response { get; set; }
public string UUID { get; set; }
}
}
Upvotes: 3
Reputation: 985
You will need Newtonsoft.Json library for this to work:
public class A
{
public string Response { get; set; }
public string UUID { get; set; }
}
static void Main(string[] args)
{
var json = "[{\"Response\":\"OK\",\"UUID\":\"89172\"}, \"Response\":\"OK\",\"UUID\":\"10304\"}]";
var result = JsonConvert.DeserializeObject<IEnumerable<A>>(json);
foreach (var a in result)
Console.WriteLine("Response: {0} UUID: {1}", a.Response, a.UUID);
Console.ReadKey();
}
Upvotes: 0
Reputation: 398
I would use Json.Net for that. Have a look at Json.Net help in the "Serializing and Deserializing JSON" section.
There they show you how to deserialize the json-string into an object.
Upvotes: 0