简体   繁体   中英

C# JSON deserializing to Custom Class

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!

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; }
 }
}

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.

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();
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM