Getting access to ArrayList object

前端 未结 2 503
天命终不由人
天命终不由人 2021-01-27 23:19

I\'m developing an application that reads some JSON data and makes some statistics, but now I\'ve run into a problem.

I have a string like this:

{
\"posi         


        
相关标签:
2条回答
  • 2021-01-27 23:56

    You can access the elements as follows:

    LinqPad Demo

    var enumerable = (IEnumerable)values["position"]; // Will get you the enumerable ArrayList
    (Dictionary<string,object>)values["position"][0]; // Will get you the First element of the ArrayList
    (string)values["position"][0]["someKey1"]; // Will get you someKey1 from the first position of position
    
    0 讨论(0)
  • 2021-01-28 00:02

    You can use Linq to JSON (add Newtonsoft's JSON.NET from NuGet):

    JObject jo = JObject.Parse(json);
    var value = (string)jo["position"][1]["someKey1"]; // someVal3
    

    Another sample:

    JObject jo = JObject.Parse(json);
    JArray positions = (JArray)jo["position"];
    foreach (var item in positions)
    {
        // use (string)item["someKey1"]
        // use (string)item["someKey2"]
    }
    
    0 讨论(0)
提交回复
热议问题