gpt4 book ai didi

c# - 复杂的json反序列化

转载 作者:行者123 更新时间:2023-11-30 17:06:18 24 4
gpt4 key购买 nike

我必须反序列化以下 json 响应(Result 列表的长度可变):

{
"ResultSet": {
"Query": "volkswagen",
"Result": [
{
"symbol": "VLKAY",
"name": "Volkswagen AG",
"exch": "PNK",
"type": "S",
"exchDisp": "OTC Markets",
"typeDisp": "Equity"
},
{
"symbol": "VOW3.DE",
"name": "Volkswagen AG",
"exch": "GER",
"type": "S",
"exchDisp": "XETRA",
"typeDisp": "Equity"
},
{
"symbol": "VOW.DE",
"name": "Volkswagen AG",
"exch": "GER",
"type": "S",
"exchDisp": "XETRA",
"typeDisp": "Equity"
}
]
}
}

我得到的:

JavaScriptSerializer js = new JavaScriptSerializer();
string jsonString = "...String is here...";
SearchObj obj = js.Deserialize<SearchObj>(jsonString);

我知道我通常必须创建一个合适的对象。例如SearchObj 将被填充,但在这种情况下,我不完全确定该对象应该是什么样子。我想到了:

class Data 
{
public string symbol { get; set; }
public string name { get; set; }
public string exch { get; set; }
public string type { get; set; }
public string exchDisp { get; set; }
public string typeDisp { get; set; }
}

class Container
{
public string Query { get; set; }
public List<Data> Result { get; set; }
}

class SearchObj
{
public Container ResultSet { get; set; }
}

但是你猜怎么着,它不起作用,我只得到 ResultSet = null

最佳答案

尝试将您的类 Container 更改为

 class Container 
{
public string Query { get; set; }
public Data[] Result { get; set; }
}

根据我的观察,我没有测试过

关于c# - 复杂的json反序列化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15476433/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com