Получаю от сервера такой JSON:
{"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"}]}
Видел 2 дня назад задавали похожий вопрос, но я пробую делать также но не выходит.
Использую такой код:
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; }
}
}
А ошибку он выдает тут foreach (AllAgent item in
temp.items) temp.items - null
Подскажите в чем ошибка, пробовал вывести reader.ReadToEnd(), выводит строку которую указал в начале, там вроде все правильно.