反序列化包含子数组的Json对象 [英] Deserialize Json Object containing sub Array

查看:115
本文介绍了反序列化包含子数组的Json对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下Json字符串(包含subArray):

I have the following Json string (containing subArray):

{属性": [{"name":"a","data":["10","0","50"],"dataName":["2000248","2789290","2789291"], "dataValue":["a","b","c"],"toClick":"d"}, {名称":"v",数据":["0","0","0","20"] ,"dataName": ["49500000","49500001","49500002","49500003"], "dataValue":["a","v","v","d"],"toClick":"d"}]}"

{"attributes": [{"name":"a","data":["10","0","50"],"dataName":["2000248","2789290","2789291"], "dataValue":["a","b","c"],"toClick":"d"}, {"name":"v","data":["0","0","0","20"] ,"dataName": ["49500000","49500001","49500002","49500003"], "dataValue":["a","v","v","d"],"toClick":"d"}]}"

我无法在csharp中反序列化. [评论后更新] 我做了什么:

I can't deserialize in csharp. [Updated after comment] What I did:

public class attributes
{
    public string name { get; set; }
    public string[] data { get; set; }
    public string[] dataName { get; set; }
    public string[] dataValue { get; set; }
    public string toClick { get; set; }
}


public class DataJsonAttributeContainer
{
    public List<attributes> JsonAttributeAfterSaves { get; set; }
}

public static T DeserializeFromJson<T>(string json)
{
    T deserializedProduct = JsonConvert.DeserializeObject<T>(json, settings);
    return deserializedProduct;
}

private void testJson()
{
    string JsonStr =
        "{\"attributes\":[{\"name\":\"a\",\"data\":[\"10\",\"0\",\"50\"],\"dataName\":[\"2000248\",\"2789290\",\"2789291\"],\"dataValue\":[\"a\",\"a\",\"d\"],\"toClick\":\"d\"},{\"name\":\"d\",\"data\":[\"0\",\"0\",\"0\",\"20\"],\"dataName\":[\"49500000\",\"49500001\",\"49500002\",\"49500003\"],\"dataValue\":[\"a\",\"a\",\"d\",\"d\"],\"toClick\":\"a\"}]}";
    var container = DeserializeFromJson<DataJsonAttributeContainer>(JsonStr);
}

测试方法:

testJson()

我该怎么办?

推荐答案

在给定JSON的情况下,DataJsonAttributeContainer中的属性名称错误.据我所知,这是一个可行的示例:

Your property within DataJsonAttributeContainer has the wrong name given your JSON. Here's an example which works as far as I can tell:

using System;
using System.Collections.Generic;
using Newtonsoft.Json;

public class Attributes
{
    public string name { get; set; }
    public string[] data { get; set; }
    public string[] dataName { get; set; }
    public string[] dataValue { get; set; }
    public string toClick { get; set; }
}


public class DataJsonAttributeContainer
{
    public List<Attributes> attributes { get; set; }
}

class Test
{

    public static T DeserializeFromJson<T>(string json)
    {
        T deserializedProduct = JsonConvert.DeserializeObject<T>(json);
        return deserializedProduct;
    }

    static void Main()
    {
        string JsonStr =
            "{\"attributes\":[{\"name\":\"a\",\"data\":[\"10\",\"0\",\"50\"],\"dataName\":[\"2000248\",\"2789290\",\"2789291\"],\"dataValue\":[\"a\",\"a\",\"d\"],\"toClick\":\"d\"},{\"name\":\"d\",\"data\":[\"0\",\"0\",\"0\",\"20\"],\"dataName\":[\"49500000\",\"49500001\",\"49500002\",\"49500003\"],\"dataValue\":[\"a\",\"a\",\"d\",\"d\"],\"toClick\":\"a\"}]}";
        var container = DeserializeFromJson<DataJsonAttributeContainer>(JsonStr);
        Console.WriteLine(container.attributes.Count); // Prints 2
        Console.WriteLine(container.attributes[0].data.Length); // Prints 3
    }
}

这篇关于反序列化包含子数组的Json对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆