使用c#的JSON数组到数组列表

loading...


1

我正在尝试将JSON转换为列表,请任何人都可以帮助我 .

public class User
{
    public String id { get; set; }
    public String imageURL { get; set; }
    public String search { get; set; }
    public String status { get; set; }
    public String username { get; set; }
}



List<User> users = JsonConvert.DeserializeObject<List<User>>(resp.Body);

JSON
{
    "KfWE8S9jWJdWnAZEbOtHTtisNwO2":
        {"id":"KfWE8S9jWJdWnAZEbOtHTtisNwO2"
        ,"imageURL":"https://firebasestorage.googleapis.com/v0/b/bchat-af5e5.appspot.com/o/uploads%2F1542785437375.jpg?alt=media&token=be1ce806-fecf-4081-9dad-f0a20e5d8489"
        ,"search":"rene vizconde"
        ,"status":"online"
        ,"username":"Rene Vizconde"},
    "ScpDnyQCyKemXSgdo3jEvZFNxY83":
        {"id":"ScpDnyQCyKemXSgdo3jEvZFNxY83"
        ,"imageURL":"default"
        ,"search":"yeli potpot"
        ,"status":"offline"
        ,"username":"Yeli Potpot"},
    "cnPYOdHYWaaLDQmchELLvw85DBf1":
        {"id":"cnPYOdHYWaaLDQmchELLvw85DBf1"
        ,"imageURL":"https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSHilM1ke9pZePBJTobMTcktggiw-UywdqAIpf-VX9nqecKl6b4wQ"
        ,"search":"bards disc"
        ,"status":"offline"
        ,"username":"Bards Disc"},
    "tWTbllTxaVM9WQnsNwnBgc3ixLM2":
        {"id":"tWTbllTxaVM9WQnsNwnBgc3ixLM2"
        ,"imageURL":"default"
        ,"search":"renz angelo"
        ,"status":"offline"
        ,"username":"Renz Angelo"}
    }

loading...

2回答

  • 0

    您可以简单地将json反序列化为 Dictionary<string, User> 而不是 List<User> .

    class Program
    {
        static void Main(string[] args)
        {
            var data = JsonConvert.DeserializeObject<Dictionary<string, User>>(resp.Body);
    
            foreach (var item in data)
            {
                User user = item.Value;
    
                Console.WriteLine("id: " + user.id);
                Console.WriteLine("imageURL: " + user.imageURL);
                Console.WriteLine("search: " + user.search);
                Console.WriteLine("status: " + user.status);
                Console.WriteLine("username: " + user.username);
                Console.WriteLine();
            }
    
            Console.ReadLine();
        }
    }
    

    Output:


  • 0

    你的JSON有问题 . 虽然它的结构“语法”正确,但它设计得很糟糕 .

    您使用ID命名JSON的成员,然后在该成员的每个对象中引用该ID .

    不要重复自己 .

    由于成员的名称是动态的,因此您无法使用该根对象来反序列化JSON . (这不是 List<User> ,JSON中没有列表,但是对象)

    请考虑使用此结构(注意括号以创建对象数组)

    [
        {
            "id":"KfWE8S9jWJdWnAZEbOtHTtisNwO2"
            ,"imageURL":"https://firebasestorage.googleapis.com/v0/b/bchat-af5e5.appspot.com/o/uploads%2F1542785437375.jpg?alt=media&token=be1ce806-fecf-4081-9dad-f0a20e5d8489"
            ,"search":"rene vizconde"
            ,"status":"online"
            ,"username":"Rene Vizconde"
        },
        {
            "id":"ScpDnyQCyKemXSgdo3jEvZFNxY83"
            ,"imageURL":"default"
            ,"search":"yeli potpot"
            ,"status":"offline"
            ,"username":"Yeli Potpot"
        },
        {
            "id":"cnPYOdHYWaaLDQmchELLvw85DBf1"
            ,"imageURL":"https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSHilM1ke9pZePBJTobMTcktggiw-UywdqAIpf-VX9nqecKl6b4wQ"
            ,"search":"bards disc"
            ,"status":"offline"
            ,"username":"Bards Disc"
        },
        {
            "id":"tWTbllTxaVM9WQnsNwnBgc3ixLM2"
            ,"imageURL":"default"
            ,"search":"renz angelo"
            ,"status":"offline"
            ,"username":"Renz Angelo"
        }
    ]
    
评论

暂时没有评论!