0

I am trying to grab all elements from flat json. I am able to see the count of the multiple items but I am unable to see the values for each list property.

The 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

Models

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; }
}

Post Method

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

I need to be able to see the values for the List properties. Thanks

LinkedListT
  • 681
  • 8
  • 24
  • 4
    Replace `public List itemid { get; set; }` by `public List itemid { get; set; }` Should be same for other list properties – Kalten Jan 09 '19 at 01:27
  • 2
    That is a very weird way to do JSON. Why is `"itemid[0]":1,` used, rather than have `itemid` pointing to an array? – mjwills Jan 09 '19 at 01:27
  • 1
    The names like `"description[0]"` are somewhat unusual... You probably looking for https://stackoverflow.com/questions/24536533/how-can-i-parse-a-json-string-that-would-cause-illegal-c-sharp-identifiers (unless you trying to represent arrays that look very different in JSON) – Alexei Levenkov Jan 09 '19 at 01:28
  • @mjwills I am using steam api, and this is how they handle json unfortunately – LinkedListT Jan 09 '19 at 01:29
  • It's the first time I see this format. But your screenshot is a proof that should work. Do you have links about this json format and/or api that use it? – Kalten Jan 09 '19 at 01:36
  • @Kalten yea, steammicrotxn api https://partner.steamgames.com/doc/webapi/ISteamMicroTxn#InitTxn – LinkedListT Jan 09 '19 at 02:00

1 Answers1

1

Everything is ok with your sample except your data model classes. You use intermediate classes (like Qtys or Items) but the json only expose array of primitive type. And not an array of object with a property XXX.

To summarize, your model only need to look like the following class :

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; }
}
Kalten
  • 4,092
  • 23
  • 32