如何用动态子代反序列化json文件?

时间:2017-12-27 08:07:51

标签: c# json xamarin xamarin.forms json.net

我有JsonFile从中获取数据,并且该文件可以像嵌套对象一样在时间上更改,因此我需要为文件中的每个更改提供解决方案

from astroid import MANAGER, register_module_extender
from astroid.builder import AstroidBuilder

def TestListener_transform():
    return AstroidBuilder(MANAGER).string_build('''
class TestListener(object):
    def get(self, timeout):
        return Peer()
''')

register_module_extender(MANAGER, 'TestListener', TestListener_transform)

# for pylint load-plugins
def register(linter):
    pass

变化可以看作例如:

{
"Result": [{
    "ProductIid": 883,
    "Mark": "KCD",
    "Position": null,
    "ChildProduct": [{
        "ProductIid": 8831,
        "Mark": "KCE",
        "Position": "AU20001",
    }]
},
{
  "ProductIid": 999,
    "Mark": "POM",
    "Position": null
},

我已经可以从这样的文件中获取数据,但是它的静态解决方案是:

{
"Result": [{
    "ProductIid": 883,
    "Mark": "KCD",
    "Position": null,
    "ChildProduct": [{
        "ProductIid": 8831,
        "Mark": "KCE",
        "Position": "AU20001",
        "ChildProduct": [{
              "ProductIid": 8832,
              "Mark": "KCF",
              "Position": "20001",
            }]
    }]
},
{
  "ProductIid": 999,
    "Mark": "POM",
    "Position": null
},

1 个答案:

答案 0 :(得分:2)

根据您提供的JSON,这看起来很简单,例如。

public class Product
{
    public long ProductIid {get;set;}
    public string Mark {get;set;}
    public string Position {get;set;}
    public Product[] ChildProduct {get;set;}
}

var data = JsonConvert.DeserializeObject<Product[]>(responseData);