Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

{
    "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}
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
116 views
Welcome To Ask or Share your Answers For Others

1 Answer

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());                    

                }*/
            }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...