JSON newtonsoft:包含字符串列表反序列化对象 [英] json newtonsoft : Deserialize Object containing a list of string

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

问题描述

我有以下问题与此JSON:

I have the following issue with this json :

{
"EVTS": {
"EVT": [
  { "ID": "123456",
    "KEY1" : "somekey",
    "CATEG": [
      "cat1",
      "cat2",
      "cat3"
    ]
  }
  ]}
  }

和这个C#类:

public class myClass{
    public string ID { get; set; }
    public string KEY1 { get; set; } 

    public list<string> CATEG { get; set; } 
}

public class ESObject1
{

    [JsonProperty("EVT")]
    public List<myClass> EVT { get; set; }
}

public class ESObject0
{

    [JsonProperty("EVTS")]
    public ESObject1 EVTS { get; set; }
}

}

在这里,我叫解串器:

ESObject0 globalobject = JsonConvert.DeserializeObject<ESObject0>(json);

不过,这最后的code不工作,我抛出这个异常: System.ArgumentException:无法施放或System.String要System.Collections.Generic.List 1 [System.String] .`

But this last code doesnt work, i throws this exception : System.ArgumentException: Could not cast or convert from System.String to System.Collections.Generic.List1[System.String].`

而不是列表&LT;字符串&GT; 我用字符串[]只有字符串似乎没有任何工作。

Instead of list<string> i used string [] and only string nothing seems to work.

我怎么能正确地反序列化这个对象吧。

how can i deserialize this object correctly please.

感谢你。

推荐答案

目前似乎没有任何明显的问题机智hyour code,因为这工作的例子说明:

There doesn't seem to be any apparent problem wit hyour code as this working example illustrates:

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

public class myClass
{
    public string ID { get; set; }
    public string KEY1 { get; set; } 
    public List<string> CATEG { get; set; } 
}

public class ESObject1
{
    [JsonProperty("EVT")]
    public List<myClass> EVT { get; set; }
}

public class ESObject0
{
    [JsonProperty("EVTS")]
    public ESObject1 EVTS { get; set; }
}


class Program
{
    static void Main()
    {
        string json = 
        @"{
            ""EVTS"": {
                ""EVT"": [
                    {
                        ""ID"": ""123456"",
                        ""KEY1"": ""somekey"",
                        ""CATEG"": [
                            ""cat1"",
                            ""cat2"",
                            ""cat3""
                        ]
                    }
                ]
            }
        }";

        ESObject0 globalobject = JsonConvert.DeserializeObject<ESObject0>(json);
        foreach (string item in globalobject.EVTS.EVT[0].CATEG)
        {
            Console.WriteLine(item);
        }
    }
}

也许你刚刚喂了错误的 JSON 值不看起来像在你的问题中所示的解串器。顺便说一句,显示的我n您的问题是无效的JSON作为你缺少一个 KEY1 财产申报。

Maybe you just fed a wrong json value to the deserializer which doesn't look like as the one shown in your question. By the way, the one shown i nyour question is invalid JSON as you are missing a , after KEY1 property declaration.

更新:

现在,你已经证明你真正的JSON(从<一个未来href="http://donnees.ville.quebec.qc.ca/Handler.ashx?id=69&f=JSON">http://donnees.ville.quebec.qc.ca/Handler.ashx?id=69&f=JSON)似乎有一个排,其中 CATEG 不是字符串数组,但一个简单的字符串:

Now that you have shown your real JSON (coming from http://donnees.ville.quebec.qc.ca/Handler.ashx?id=69&f=JSON) it appears that there's a row where CATEG is not an array of strings but a simple string:

""CATEG"": ""Conférence""

现在,这是一个pretty的糟糕的设计,因为它们是混合阵列和简单性。恐怕,为了应对这种情况,你就需要使用 JObject 和提取你需要通过测试实际的基础类型的信息。

Now that's a pretty bad design because they are mixing arrays and simple properties. I am afraid that in order to deal with this situation you will need to use JObjects and extract the information you need by testing the actual underlying type.

例如:

var obj = JObject.Parse(json);
var events = (JArray)obj["EVTS"]["EVT"];
foreach (JObject evt in events)
{
    var categories = evt["CATEG"];
    if (categories is JArray)
    {
        // you've got a list of strings so you can loop through them
        string[] cats = ((JArray)categories)
            .Select(x => x.Value<string>())
            .ToArray();
    }
    else
    {
        // you've got a simple string
        string cat = categories.Value<string>();
    }
}

这篇关于JSON newtonsoft:包含字符串列表反序列化对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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