将JSON单个字符串反序列化为数组

时间:2015-07-02 14:01:51

标签: c# json

我正在尝试反序列化我的json数据但是我的类没有正确设置我无权更改json响应,所以我需要编写一个函数来正确处理json。

这是数据

{
  "blabla": {
    "-Score": "1",
    "-Ref": "50",
    "foo": {
      "-colour": "Yellow",
      "-ref": "y50"
    }
  }
}

但有时数据将是

    {
  "blabla": {
    "-Score": "1",
    "-Ref": "50",
    "foo": [
      {
        "-colour": "Yellow",
        "-ref": "y50"
      },
      {
        "-colour": "Green",
        "-ref": "g50"
      },
      {
        "-colour": "Red",
        "-ref": "r50"
      }
    ]
  }
}

此类适用于第一个数据

public class blabla
{
    public Foo Foo {get; set;}
}

此课程适用于第二个数据

public class blabla
{
    public Foo[] Foo {get; set;}
}

但是我怎样才能让班级为两者工作?

2 个答案:

答案 0 :(得分:3)

以下是基本课程:

public class Test
{
    public Blabla blabla { get; set; }
}

public class Blabla
{
    public string _score { get; set; }
    public string _ref { get; set; }

    [JsonConverter(typeof(FooConverter))]
    public Foo[] foo { get; set; }
}

public class Foo
{
    public string _colour { get; set; }
    public string _ref { get; set; }
}

无论数据是什么,都将foo的类型设置为Foo[],并添加[JsonConverter(typeof(FooConverter))]以使用自定义转换器。

这是自定义转换器:

public class FooConverter : JsonConverter
{
    // Declared as abstract in JsonConverter so must be overridden
    public override bool CanConvert(Type objectType) { return true; }

    // Declared as abstract in JsonConverter so must be overridden
    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { }

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        JToken token = JToken.Load(reader);

        return token.Type == JToken.Array ? token.ToObject<Foo[]>() : new Foo[] { token.ToObject<Foo>() };
    }
}

ReadJson方法中,我们在令牌中加载数据,然后检查数据是数组还是普通对象。如果它已经是一个数组,我们只需将数组对象作为Foo[]返回,如果它是一个普通对象,我们返回一个包含普通对象的new Foo[]

这是一个测试用例:

string json1 = @"{
                  ""blabla"": 
                    {
                      ""_score"": ""1"",
                      ""_ref"": ""50"",
                      ""foo"": 
                        {
                          ""_colour"": ""Yellow"",
                          ""_ref"": ""y50""
                        }
                    }
                }";

string json2 = @"{
                 ""blabla"": 
                    {
                      ""_score"": ""1"",
                      ""_ref"": ""50"",
                      ""foo"": 
                        [
                          {
                            ""_colour"": ""Yellow"",
                            ""_ref"": ""y50""
                          },
                          {
                            ""_colour"": ""Green"",
                            ""_ref"": ""g50""
                          },
                          {
                            ""_colour"": ""Red"",
                            ""_ref"": ""r50""
                          }
                        ]
                    }
                }";

Test test1 = JsonConvert.DeserializeObject<Test>(json1);
Test test2 = JsonConvert.DeserializeObject<Test>(json2);

您将始终拥有一个数组,但第一个测试用例中将有1个元素,第二个测试用例中将有3个元素。

答案 1 :(得分:1)

  1. 转到json2csharp.com
  2. 粘贴您的json数据
  3. 生成课程
  4. 你去:)

    id:1->weight:12
    id:2->weight:7