简体   繁体   English

将C#JSON反序列化为自定义类

[英]C# JSON deserializing to Custom Class

I would like to deserialize a JSON object like this: 我想反序列化这样的JSON对象:

[{"Response":"OK","UUID":"89172"},{"Response":"OK","UUID":"10304"}]

into a custom class where it has variables storing Response and UUID . 到一个自定义类中,该类具有用于存储ResponseUUID变量。 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. 如果我可以使用方法ForEach以便可以相应地弹出数据,那就太好了。 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 然后您可以使用库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. 我会使用Json.Net。 Have a look at Json.Net help in the "Serializing and Deserializing JSON" section. 在“ JSON序列化和反序列化”部分中查看Json.Net帮助

There they show you how to deserialize the json-string into an object. 在那里,他们向您展示了如何将json-string反序列化为一个对象。

You will need Newtonsoft.Json library for this to work: 您将需要Newtonsoft.Json库来使其工作:

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. 感谢@Newton Sheikh的帮助,我终于解决了这个问题。 Thank you first of all. 首先谢谢你。

First I created a class ( Student ) 首先,我创建了一个班级( Student

public class Student
{
   public string Response { get; set; }
   public string UUID { get; set; }
}

Then I imported the JSON.NET and created a function: 然后,我导入了JSON.NET并创建了一个函数:

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! 谢谢@Newton Sheikh和其他人的帮助! I hope this example code can help others too! 我希望这个示例代码也可以帮助其他人! Cheers. 干杯。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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