如何解析不完全对应的json [英] How to parse not fully corresponding json

查看:149
本文介绍了如何解析不完全对应的json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用第三方API,因此无法更改响应结构. 作为回应,我得到这样的东西:

I am using third-party API, so I cannot change the response structure. In response I am getting something like this:

{
    "Code": "SomeCode",
    "Name": "Some Name",
    "IsActive": true,
    "Prop13": {
        "LongId": "12",
        "ShortId": "45"
    },
    "Prop26": {
        "LongId": "12",
        "ShortId": "45"
    },
    "Prop756": {
        "LongId": "12",
        "ShortId": "45"
    }
}

我需要将其解析为下一个类:

I need to parse it to next type of class:

public class Class1
{
    public string Code { get; set; }

    public string Name { get; set; }

    public bool IsActive { get; set; }

    public Dictionary<string, PropertiesClass> Properties { get; set; }
}

public class PropertiesClass
{
    public int LongId { get; set; }

    public int ShortId { get; set; }
}

属性"Prop13","Prop26"等是动态响应的,可能都不存在,或者名称完全不同.因此,我必须仅将Code,Name和IsActive(始终存在)存储到类属性中,所有其他属性都应存储在Dictionary中.属性名称应作为键存储在字典中.

Properties "Prop13", "Prop26" etc are dynamic in response, there could none of them, or with completely other name. So I must store only Code, Name and IsActive (this are always present) to class properties, all other should go to the Dictionary. And the name of the property should be stored as a key in the dictionary.

https://www中找不到任何可以帮助我的东西. newtonsoft.com/json/help/html/Introduction.htm

推荐答案

我可以想到一种方法.

您的Class1需要稍作修改:

public class Class1
{
    public string Code { get; set; }

    public string Name { get; set; }

    public bool IsActive { get; set; }

    [JsonExtensionData]
    public Dictionary<string, JToken> _JTokenProperty { get; set; }

    public Dictionary<string, PropertiesClass> Properties1 { get; set; } = new Dictionary<string, PropertiesClass>();
}

然后在解析对象的位置,您要像这样对它进行解析:

Then where you parse the object, you want to pare it like so:

var obj = JsonConvert.DeserializeObject<Class1>("{\"Code\":\"SomeCode\",\"Name\":\"Some Name\",\"IsActive\":true,\"Prop13\":{\"LongId\":\"12\",\"ShortId\":\"45\"},\"Prop26\":{\"LongId\":\"12\",\"ShortId\":\"45\"},\"Prop756\":{\"LongId\":\"12\",\"ShortId\":\"45\"}}");

foreach(KeyValuePair<string, JToken> token in obj._JTokenProperty)
{
    obj.Properties1.Add(token.Key, token.Value.ToObject<PropertiesClass>());
}

这将生成所需的输出.

感谢@Nkosi提供的链接和建议,使其保持独立. 您可以将以下内容添加到Class1

Thanks to @Nkosi for the link and suggestion to keep it self-contained. You would add the following to Class1

[OnDeserialized]
private void OnDeserialized(StreamingContext context)
{

    foreach (KeyValuePair<string, JToken> token in _JTokenProperty)
    {
        Properties1.Add(token.Key, token.Value.ToObject<PropertiesClass>());
    }
}

您的反序列化变为:

var obj = JsonConvert.DeserializeObject<Class1>("{\"Code\":\"SomeCode\",\"Name\":\"Some Name\",\"IsActive\":true,\"Prop13\":{\"LongId\":\"12\",\"ShortId\":\"45\"},\"Prop26\":{\"LongId\":\"12\",\"ShortId\":\"45\"},\"Prop756\":{\"LongId\":\"12\",\"ShortId\":\"45\"}}");

这篇关于如何解析不完全对应的json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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