Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将JSON对象反序列化为数组_C#_Json_Json.net - Fatal编程技术网

C# 将JSON对象反序列化为数组

C# 将JSON对象反序列化为数组,c#,json,json.net,C#,Json,Json.net,我们有一个JSON结构,其中我们将键值对数组从服务器发送到客户端。为了简洁和整洁,我们发送如下数据: "identification": { "lineItem/latestDate": "2013-04-28", "lineItem/destination": "test", "lineItem/quantity": "55" } 与此相反: "identification": [ { "value": "test", "typ

我们有一个JSON结构,其中我们将键值对数组从服务器发送到客户端。为了简洁和整洁,我们发送如下数据:

"identification": {
    "lineItem/latestDate": "2013-04-28",
    "lineItem/destination": "test",
    "lineItem/quantity": "55"
}
与此相反:

"identification": [
    {
        "value": "test",
        "type": "lineItem/destination"
    },
    {
        "value": "2014-07-25",
        "type": "lineItem/latestDate"
    },
    {
        "value": "55",
        "type": "lineItem/quantity"
    }
]
问题是我们的下游客户机(正在使用C#处理JSON)说他们无法处理第一种格式,因为内置的JSON.net功能不支持它,而且他们不知道还有其他可以处理的格式

是否可以使用C#中内置的JSON反序列化来反序列化第一种格式?或者有人知道另一个图书馆可以处理这个问题吗?我看了一下,对我来说这是可能的。

1)给定的第一个JSON无效,需要在开始处使用左大括号,并从最后一个逗号中删除一个额外的逗号

2) 如果您的客户机可以使用Newtonsoft.Json,并且您可以在键中使用“\而不是“/”,因为在c中不支持名称中带有“/”的标识符#

客户端:

将类创建为

public class Identification
{
    public string lineItem_latestDate { get; set; }
    public string lineItem_destination { get; set; }
    public string lineItem_quantity { get; set; }
}

public class RootObject
{
    public Identification identification { get; set; }
}
注意:如果有更多的键/值对,则可以向类标识添加更多属性

现在,将其反序列化为:

        string data = @"[
{
    ""identification"": {
        ""lineItem_latestDate"": ""2013-04-28"",
        ""lineItem_destination"": ""test"",
        ""lineItem_quantity"": ""55""
    }
},
{
    ""identification"": {
        ""lineItem_latestDate"": ""2013-04-28"",
        ""lineItem_destination"": ""test"",
        ""lineItem_quantity"": ""55""
    }
},
{
    ""identification"": {
        ""lineItem_latestDate"": ""2013-04-28"",
        ""lineItem_destination"": ""test"",
        ""lineItem_quantity"": ""55""
    }
}]";

        List<RootObject> ob = JsonConvert.DeserializeObject<List<RootObject>>(data);
字符串数据=@”[
{
“识别”:{
“lineItem_latestDate:”“2013-04-28”,
“lineItem_destination”“:”“test”“,
“行项目数量”:“55”
}
},
{
“识别”:{
“lineItem_latestDate:”“2013-04-28”,
“lineItem_destination”“:”“test”“,
“行项目数量”:“55”
}
},
{
“识别”:{
“lineItem_latestDate:”“2013-04-28”,
“lineItem_destination”“:”“test”“,
“行项目数量”:“55”
}
}]";
List ob=JsonConvert.DeserializeObject(数据);

完成,现在ob对象根据数据具有值。

Json.Net确实支持反序列化键值对:您只需要使用
字典
,其中
T
可以是
object
JToken
或其他一些可以接收数据值的类(
string
)。使用字典,反序列化和迭代值实际上非常容易,如下所示:

class Program
{
    static void Main(string[] args)
    {
        string json = @"
        {
            ""identification"": {
                ""lineItem/latestDate"": ""2013-04-28"",
                ""lineItem/destination"": ""test"",
                ""lineItem/quantity"": ""55"",
            }
        }";

        RootObject obj = JsonConvert.DeserializeObject<RootObject>(json);

        foreach (KeyValuePair<string, string> kvp in obj.identification)
        {
            Console.WriteLine("type: " + kvp.Key);
            Console.WriteLine("value: " + kvp.Value);
            Console.WriteLine();
        }
    }
}

class RootObject
{
    public Dictionary<string, string> identification { get; set; }
}

在这里,我找到了另一种方法,好消息是我们不必更改密钥名称

在客户端创建一个类:

public class RootObject
{
    public Dictionary<string,string> identification { get; set; }
}
公共类根对象
{
公共字典标识{get;set;}
}
现在,将其反序列化:

        string data = @"[
{
    ""identification"": {
        ""lineItem_latestDate"": ""2013-04-28"",
        ""lineItem_destination"": ""test"",
        ""lineItem_quantity"": ""55""
    }
},
{
    ""identification"": {
        ""lineItem_latestDate"": ""2013-04-28"",
        ""lineItem_destination"": ""test"",
        ""lineItem_quantity"": ""55""
    }
},
{
    ""identification"": {
        ""lineItem_latestDate"": ""2013-04-28"",
        ""lineItem_destination"": ""test"",
        ""lineItem_quantity"": ""55""
    }
}]";

List<RootObject> ob = JsonConvert.DeserializeObject<List<RootObject>>(data);
字符串数据=@”[
{
“识别”:{
“lineItem_latestDate:”“2013-04-28”,
“lineItem_destination”“:”“test”“,
“行项目数量”:“55”
}
},
{
“识别”:{
“lineItem_latestDate:”“2013-04-28”,
“lineItem_destination”“:”“test”“,
“行项目数量”:“55”
}
},
{
“识别”:{
“lineItem_latestDate:”“2013-04-28”,
“lineItem_destination”“:”“test”“,
“行项目数量”:“55”
}
}]";
List ob=JsonConvert.DeserializeObject(数据);
宾果,我们又做了一次

1) 现在,ob对象具有所需的值

2) 使用值数组


3) 也支持迭代。

根对象不应该包含公共列表标识{get;set;}?根据标题,JSON字符串应该反序列化为数组。感谢您注意到这一点,我不知道我是如何跳过它的。如果它是一个数组,它应该是列表,我将更新它,未编码的行项目会发生什么情况?也就是说,如果消费者想重复这些项目?这种方式比字典删除错误的逗号要快一点,删除数据时必须保留逗号,以保持问题的简单性。删除我的帖子,因为这些新信息证明我错了。非常感谢。
        string data = @"[
{
    ""identification"": {
        ""lineItem_latestDate"": ""2013-04-28"",
        ""lineItem_destination"": ""test"",
        ""lineItem_quantity"": ""55""
    }
},
{
    ""identification"": {
        ""lineItem_latestDate"": ""2013-04-28"",
        ""lineItem_destination"": ""test"",
        ""lineItem_quantity"": ""55""
    }
},
{
    ""identification"": {
        ""lineItem_latestDate"": ""2013-04-28"",
        ""lineItem_destination"": ""test"",
        ""lineItem_quantity"": ""55""
    }
}]";

List<RootObject> ob = JsonConvert.DeserializeObject<List<RootObject>>(data);