-1
{
    "12": {
        "_agicId": 2,
        "_index_": "",
        "_seq": 1
    },
    "11": {
        "_agicId": 1,
        "_index_": "",
        "_seq": 2
    },
    "10": {
        "_agicId": 0,
        "_index_": "",
        "_seq": 3
    }
}

I get a json string like above, but i don't know "12""11""10", how can i parser this json get _seq & agicId?

var Name = JObject.Parse(r);
int Count = Name.Count;
for (int i = 0; i < Name.Count; i++)
{
    // how can i get  "11"{"agicId":0}
}
superlevin
  • 21
  • 2
  • 1
    what have you ***tried yourself*** so far? what problems did you encounter? what have you researched? it is currently quite unclear what your goal and your problem actually _are_. i recommend [taking the tour](https://stackoverflow.com/tour), as well as reading [how to ask a good question](https://stackoverflow.com/help/how-to-ask) and [what's on topic](https://stackoverflow.com/help/on-topic). – Franz Gleichmann Jul 05 '21 at 08:50
  • 2
    This is a dictionary, just deserialize it as such – TheGeneral Jul 05 '21 at 08:56
  • Does this answer your question? [json deserialization to C#](https://stackoverflow.com/questions/65727513/json-deserialization-to-c-sharp) Deserialize into Dictionary – Charlieface Jul 05 '21 at 09:39

1 Answers1

1

For this create a model class like below:

class Test
    {
        public int _agicId { get; set; }
        public string _index_ { get; set; }
        public string _seq { get; set; }
    }

And then read the json like below. I have read from file . you can read from string also. In the key you will get 11, 12 etc...and in value you will get the Text class having values for "agicId" etc...

using (StreamReader r = new StreamReader("data.json"))
            {
                string jsonString = r.ReadToEnd();
                JObject jsonObj = JObject.Parse(jsonString);

                var k = JsonConvert.DeserializeObject<Dictionary<string, Test>>(jsonString);
                /*
                A very good suggestion by Jon Skeet. No need of this foreach loop. Instead of criticising he has suggested a better solution.
                foreach(var item in jsonObj)
                {
                    string key = item.Key;
                    var value =  JsonConvert.DeserializeObject<Test>(item.Value.ToString());                    

                }*/
            }
Amit Verma
  • 2,450
  • 2
  • 8
  • 21