D
D
Dmitry2015-12-26 23:57:54
JSON
Dmitry, 2015-12-26 23:57:54

How to use json array in C#?

I get this JSON from the server:

{"agent":[{"ID":"1","Surname":"\u0418\u0432\u0430\u043d\u043e\u0432","Name":"\u0418\u0432\u0430\u043d","MiddleName":"\u0418\u0432\u0430\u043d\u043e\u0432\u0438\u0447","Post":"1"},{"ID":"2","Surname":"testName=tes","Name":"","MiddleName":"\u0418\u0432\u0430\u043d\u043e\u0432\u0438\u0447","Post":"1"},{"ID":"3","Surname":"test2 Name=test2","Name":"","MiddleName":"\u0418\u0432\u0430\u043d\u043e\u0432\u0438\u0447","Post":"1"},{"ID":"4","Surname":"test3,Name=test3","Name":"","MiddleName":"\u0418\u0432\u0430\u043d\u043e\u0432\u0438\u0447","Post":"1"},{"ID":"5","Surname":"test4 , Name=test4","Name":"","MiddleName":"\u0418\u0432\u0430\u043d\u043e\u0432\u0438\u0447","Post":"1"},{"ID":"6","Surname":"test5 ","Name":"test5","MiddleName":"\u0418\u0432\u0430\u043d\u043e\u0432\u0438\u0447","Post":"1"}]}

I saw a similar question asked 2 days ago, but I try to do the same but it doesn't work.
I use this code:
public static void MySQLPreDelAgent()
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://dima-n.16mb.com/Okna/PHP/set_all_agent.php");
            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";
            request.Accept = "Accept=text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";


            HttpWebResponse response = (HttpWebResponse)request.GetResponse();

            using (StreamReader reader = new StreamReader(response.GetResponseStream()))
            {
                //Console.WriteLine(reader.ReadToEnd());
                var temp = (MyArray)JsonConvert.DeserializeObject(reader.ReadToEnd(), typeof(MyArray));
                foreach (AllAgent item in temp.items)
                {
                    Console.WriteLine("Key: {0}, Value {1}", item.ID , item.MiddleName);
                }
            }
        }
        public class MyArray
        {
            public List<AllAgent> items { get; set; }
        }

        public class AllAgent
        {
            public int ID { get; set; }
            public string Surname { get; set; }
            public string Name { get; set; }
            public string MiddleName { get; set; }
            public int Post { get; set; }
        }
}

And it gives an error here foreach (AllAgent item in temp.items ) temp.items - null Tell me
what the error is, I tried to display reader.ReadToEnd(), it displays the line that I indicated at the beginning, everything seems to be correct there.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry, 2015-12-27
@dim9044

I realized that I don’t have items but an agent)) I don’t have any attention already)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question