C# JSON Parsing

前端 未结 2 491
猫巷女王i
猫巷女王i 2020-12-06 23:51

I have problems parsing some JSON data:

{
    \"title\": \"LED SIDE DIRECTION INDICATOR 9-33V CATEGORY 6, 19cm CABLE CHROME BASE  LED AUTO LAMPS\",
    \"us         


        
相关标签:
2条回答
  • 2020-12-07 00:37

    This works with your json

    var dict = new JavaScriptSerializer().Deserialize<Dictionary<string, object>>(testJson);
    
    Console.WriteLine(dict["url"]);
    
    var price = (Dictionary<string, object>)dict["user_price"];
    Console.WriteLine(price["inc_tax_value"]);
    
    0 讨论(0)
  • 2020-12-07 00:39

    If the fields of the json object are always the same, then you could use strong typed classes instead of dictionaries. The json serializer will serialize correctly to the following classes:

    private class MyJsonClass
    {
        public string title { get; set; }
        public UserPrice user_price { get; set; }
        public int local_quantity { get; set; }
        ...
    }
    
    private class UserPrice
    {
        public decimal inc_tax_value { get; set; }
        public decimal ex_tax_value { get; set; }
        ...
    }
    
    0 讨论(0)
提交回复
热议问题