简体   繁体   English

如何处理json对象中的json对象c#

[英]how to handle json object inside json object c#

I am sending URL request and getting response in curl and then convert into a json...object inside object(contain numeric and dot(924.136028459)) like:我正在发送 URL 请求并在 curl 中获取响应,然后转换为 json...object 内的对象(包含数字和点(924.136028459)),例如:

string arr1 = 
    "{  
       "success":1,
       "results":[  
          {  
             "Markets":{  
                "924.136028459":{  
                   "productType":"BOOK1",
                   "key":"SB_MARKET:924.136028459"
                },
                "924.136028500":{  
                   "productType":"BOOK2",
                   "key":"SB_MARKET:924.136028459"
                }
             }
          }
       ]
    }";

I have created properties class ..but i am not understanding how can we access inside "924.136028500" attributes我创建了属性类..但我不明白我们如何访问“924.136028500”属性

public class Json
{
    public System.Collections.ObjectModel.Collection<Arr> results { get; set; }
}

public class Arr
{
    public sp Markets { get; set; }
}

public class sp
{
    public string productType { get; set; }
    public string key { get; set; }
}

and I am using deserialize code...我正在使用反序列化代码...

var serializer = new JavaScriptSerializer();
Json json = serializer.Deserialize<Json>(arr1);

With Cinchoo ETL - an open source library, you can load the Market object easily with few lines of code使用Cinchoo ETL - 一个开源库,您可以使用几行代码轻松加载 Market 对象

string json = @"{  
       ""success"":1,
       ""results"":[
          {  
             ""Markets"":{  
                ""924.136028459"":{  
                   ""productType"":""BOOK1"",
                   ""key"":""SB_MARKET:924.136028459""

                },
                ""924.136028500"":{  
                   ""productType"":""BOOK2"",
                   ""key"":""SB_MARKET:924.136028459""
                }
             }
          }
       ]
    }
";

foreach (var rec in ChoJSONReader<sp>.LoadText(json).WithJSONPath("$..Markets.*"))
    Console.WriteLine($"ProductType: {rec.productType}, Key: {rec.key}");

Output:输出:

ProductType: BOOK1, Key: SB_MARKET:924.136028459
ProductType: BOOK2, Key: SB_MARKET:924.136028459

Checkout CodeProject article for some additional help.查看 CodeProject 文章以获得一些额外的帮助。

Disclaimer: I'm the author of this library.免责声明:我是这个库的作者。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM