我有一个JSON响应,如下所示:
{
"success": true,
"more": false,
"results_html": "a lot of html in here",
"listinginfo": {
"637640274112277168": {
"listingid": "637640274112277168",
"price": 50,
"fee": 7,
"publisher_fee_app": 730,
"publisher_fee_percent": "0.10000000149011612",
"currencyid": "2005",
"steam_fee": 2,
"publisher_fee": 5,
"converted_price": 1,
"converted_fee": 2,
"converted_currencyid": "2003",
"converted_steam_fee": 1,
"converted_publisher_fee": 1,
"converted_price_per_unit": 1,
"converted_fee_per_unit": 2,
"converted_steam_fee_per_unit": 1,
"converted_publisher_fee_per_unit": 1,
"asset": {
"currency": 0,
"appid": 730,
"contextid": "2",
"id": "6542776191",
"amount": "1"
}
},
"194035710805671301": {
"listingid": "194035710805671301",
"price": 0,
"fee": 0,
"publisher_fee_app": 730,
"publisher_fee_percent": "0.10000000149011612",
"currencyid": "2001",
"asset": {
"currency": 0,
"appid": 730,
"contextid": "2",
"id": "6825071309",
"amount": "0"
}
},
}//end listinginfo
//more fields here..
}
我需要从&#34; listinginfo&#34;获取信息。节点并将它们添加到 dictionary<string, NewListedItem>
,其中字符串将是例如:&#34; 637640274112277168&#34;,该列表的类型为&#34; NewListedItem&#34;,包含该子节点的所有信息。我的课程看起来像这样:
class listinginfo
{
public Dictionary<string, NewListedItem> Items;
}
class Asset
{
public string currency { get; set; }
public string appid { get; set; }
public string contextid { get; set; }
public string id { get; set; }
public string amount { get; set; }
}
class NewListedItem
{
public string listingid { get; set; }
public string price { get; set; }
public string fee { get; set; }
public string publisher_fee_app { get; set; }
public string publisher_fee_percent { get; set; }
public string steam_fee { get; set; }
public string publisher_fee { get; set; }
public string converted_price { get; set; }
public string converted_fee { get; set; }
public string converted_currencyid { get; set; }
public string converted_steam_fee { get; set; }
public string converted_publisher_fee { get; set; }
public string converted_fee_per_unit { get; set; }
public string converted_steam_fee_per_unit { get; set; }
public string converted_publisher_fee_per_unit { get; set; }
public Asset asset { get; set; }
}
我怎样才能实现这个目标?
答案 0 :(得分:1)
所以我无法以干净的方式做到这一点,但我相信这正是你要找的:
var jsonString = @"{
'success': true,
'more': false,
'results_html': 'a lot of html in here',
'listinginfo': {
'637640274112277168': {
'listingid': '637640274112277168',
'price': 50,
'fee': 7,
'publisher_fee_app': 730,
'publisher_fee_percent': '0.10000000149011612',
'currencyid': '2005',
'steam_fee': 2,
'publisher_fee': 5,
'converted_price': 1,
'converted_fee': 2,
'converted_currencyid': '2003',
'converted_steam_fee': 1,
'converted_publisher_fee': 1,
'converted_price_per_unit': 1,
'converted_fee_per_unit': 2,
'converted_steam_fee_per_unit': 1,
'converted_publisher_fee_per_unit': 1,
'asset': {
'currency': 0,
'appid': 730,
'contextid': '2',
'id': '6542776191',
'amount': '1'
}
},
'194035710805671301': {
'listingid': '194035710805671301',
'price': 0,
'fee': 0,
'publisher_fee_app': 730,
'publisher_fee_percent': '0.10000000149011612',
'currencyid': '2001',
'asset': {
'currency': 0,
'appid': 730,
'contextid': '2',
'id': '6825071309',
'amount': '0'
}
},
}
}";
var json = JObject.Parse(jsonString);
List<JToken> results = json["listinginfo"].Children().Children().ToList();
var dictionaryResults = new ListingInfo();
foreach (var token in results)
{
var info = JsonConvert.DeserializeObject<NewListedItem>(token.ToString());
List<NewListedItem> listInfo = new List<NewListedItem>();
listInfo.Add(info);
dictionaryResults.Items = new Dictionary<string, List<NewListedItem>>();
dictionaryResults.Items.Add(info.Listingid, listInfo);
}
我还修改了你的类的属性名称:
public class Asset
{
public string Currency { get; set; }
public string Appid { get; set; }
public string Contextid { get; set; }
public string Id { get; set; }
public string Amount { get; set; }
}
public class NewListedItem
{
public string Listingid { get; set; }
public string Price { get; set; }
public string Fee { get; set; }
[JsonProperty("publisher_fee_app")]
public string PublisherFeeApp { get; set; }
[JsonProperty("publisher_fee_percent")]
public string PublisherFeePercent { get; set; }
[JsonProperty("steam_fee")]
public string SteamFee { get; set; }
[JsonProperty("publisher_fee")]
public string PublisherFee { get; set; }
[JsonProperty("converted_price")]
public string ConvertedPrice { get; set; }
[JsonProperty("converted_fee")]
public string ConvertedFee { get; set; }
[JsonProperty("converted_currencyid")]
public string ConvertedCurrencyid { get; set; }
[JsonProperty("converted_steam_fee")]
public string ConvertedSteamFee { get; set; }
[JsonProperty("converted_publisher_fee")]
public string ConvertedPublisherFee { get; set; }
[JsonProperty("converted_fee_per_unit")]
public string ConvertedFeePerUnit { get; set; }
[JsonProperty("converted_steam_fee_per_unit")]
public string ConvertedSteamFeePerUnit { get; set; }
[JsonProperty("converted_publisher_fee_per_unit")]
public string ConvertedPublisherFeePerUnit { get; set; }
public Asset Asset { get; set; }
}
答案 1 :(得分:1)
你真的很亲密。除非您需要将根类中的字典属性的名称从Items
更改为listinginfo
以镜像JSON属性名称,否则您的类将在您定义它们时正常工作。 (或者,您可以使用[JsonProperty]
属性将Items
字典映射到JSON中的listinginfo
属性。)进行此更改后,我还建议您重命名根类{ {1}}对你有意义的其他事情,比如listinginfo
,但这不是绝对必要的。
在这些更改之后,您的根类应如下所示:
ListingResponse
或者,如果您更喜欢属性方法:
public class ListingResponse
{
public Dictionary<string, NewListedItem> listinginfo { get; set; }
}
然后,您应该能够将JSON反序列化到此类中,并且您的字典将按预期填充:
public class ListingResponse
{
[JsonProperty("listinginfo")]
public Dictionary<string, NewListedItem> Items { get; set; }
}
答案 2 :(得分:0)
public class TestClass
{
private void Test()
{
var json = "{\r\n \"637640274112277168\": {\r\n \"listingid\": \"637640274112277168\",\r\n \"price\": 50,\r\n \"fee\": 7,\r\n \"publisher_fee_app\": 730,\r\n \"publisher_fee_percent\": \"0.10000000149011612\",\r\n \"currencyid\": \"2005\",\r\n \"steam_fee\": 2,\r\n \"publisher_fee\": 5,\r\n \"converted_price\": 1,\r\n \"converted_fee\": 2,\r\n \"converted_currencyid\": \"2003\",\r\n \"converted_steam_fee\": 1,\r\n \"converted_publisher_fee\": 1,\r\n \"converted_price_per_unit\": 1,\r\n \"converted_fee_per_unit\": 2,\r\n \"converted_steam_fee_per_unit\": 1,\r\n \"converted_publisher_fee_per_unit\": 1,\r\n \"asset\": {\r\n \"currency\": 0,\r\n \"appid\": 730,\r\n \"contextid\": \"2\",\r\n \"id\": \"6542776191\",\r\n \"amount\": \"1\"\r\n }\r\n },\r\n \"194035710805671301\": {\r\n \"listingid\": \"194035710805671301\",\r\n \"price\": 0,\r\n \"fee\": 0,\r\n \"publisher_fee_app\": 730,\r\n \"publisher_fee_percent\": \"0.10000000149011612\",\r\n \"currencyid\": \"2001\",\r\n \"asset\": {\r\n \"currency\": 0,\r\n \"appid\": 730,\r\n \"contextid\": \"2\",\r\n \"id\": \"6825071309\",\r\n \"amount\": \"0\"\r\n }\r\n }\r\n }\r\n\r\n";
Dictionary<string, NewListedItem> values = JsonConvert.DeserializeObject<Dictionary<string, NewListedItem>>(json);
}
}
class listinginfo
{
public Dictionary<string, List<NewListedItem>> Items;
}
class Asset
{
public string currency { get; set; }
public string appid { get; set; }
public string contextid { get; set; }
public string id { get; set; }
public string amount { get; set; }
}
class NewListedItem
{
public string listingid { get; set; }
public string price { get; set; }
public string fee { get; set; }
public string publisher_fee_app { get; set; }
public string publisher_fee_percent { get; set; }
public string steam_fee { get; set; }
public string publisher_fee { get; set; }
public string converted_price { get; set; }
public string converted_fee { get; set; }
public string converted_currencyid { get; set; }
public string converted_steam_fee { get; set; }
public string converted_publisher_fee { get; set; }
public string converted_fee_per_unit { get; set; }
public string converted_steam_fee_per_unit { get; set; }
public string converted_publisher_fee_per_unit { get; set; }
public Asset asset { get; set; }
}