反序列化平面JSON C#

时间:2019-01-09 01:22:07

标签: c# json

我正在尝试从平面json中获取所有元素。我可以看到多个项目的计数,但是看不到每个列表属性的值。

JSON:

    {  
   "Key":"C23432151461561",
   "OrderId": 129012092109,
   "SteamId":12341234321,
   "AppId":1234132,
   "ItemCount":2,
   "Language":"en",
   "Currency":"CAD",
   "itemid[0]":1,
   "qty[0]":2,
   "amount[0]":12,
   "description[0]":"majora's mask",
   "itemid[1]":1,
   "qty[1]":2,
   "amount[1]":12,
   "description[1]":"mario's hat",
    }

enter image description here

模型

class Descriptions
{
    public string descriptions { get; set; }
}

public class Amounts
{
    public int amounts { get; set; }
}

public class Qtys
{
    public int qtys { get; set; }
}

public class Items
{
    public int itemids { get; set; }
}
public class InitTxn
{
    public string key { get; set; }
    public int orderid { get; set; }
    public long steamid { get; set; }
    public int appid { get; set; }
    public int itemcount { get; set; }
    public string language { get; set; }
    public string currency { get; set; }
    public List<Items> itemid { get; set; }
    public List<Qtys> qty { get; set; }
    public List<Amounts> amount { get; set; }
    public List<Descriptions> description { get; set; }
}

发布方法

[System.Web.Http.HttpPost]
public string InitRequest([FromBody] InitTxn initTxn)
    {}

我需要能够看到List属性的值。谢谢

1 个答案:

答案 0 :(得分:1)

您的示例一切正常,但数据模型类除外。您使用中间类(例如QtysItems),但是json仅公开原始类型的数组。而不是具有属性XXX的对象数组。

总而言之,您的模型只需要看起来像下面的类:

public class InitTxn
{
    public string key { get; set; }
    public int orderid { get; set; }
    public long steamid { get; set; }
    public int appid { get; set; }
    public int itemcount { get; set; }
    public string language { get; set; }
    public string currency { get; set; }
    public List<int> itemid { get; set; }
    public List<int> qty { get; set; }
    public List<int> amount { get; set; }
    public List<string> description { get; set; }
}