JSON получить нужные параметры - C#
Формулировка задачи:
Ответ сервера
Создал класс с помощью VS
Пытаюсь получить нужный параметр по примеру http://mustknowthat.blogspot.com.by/2013/06/json-c.html
Но не могу получить нужные параметры. Подскажите или дайте пример, как использовать JSON.
{
"results":[
{
"gender":"male",
"name":{
"title":"mr",
"first":"priam",
"last":"fernandes"
},
"location":{
"street":"1775 rua onze ",
"city":"santarГ©m",
"state":"rio grande do sul",
"postcode":28095
},
"email":"priam.fernandes@example.com",
"login":{
"username":"blackdog828",
"password":"bogart",
"salt":"Hc5YYAkB",
"md5":"55bddb660f4289b2e22d99c260c20201",
"sha1":"761578b47d4a1f60f08fdd663b3cb9d39ee60459",
"sha256":"140de2307eea8ae9ce6f2b76879fdf757662014c835cb1a99e90aa7abc8eff33"
},
"registered":1065541913,
"dob":1362174365,
"phone":"(32) 0157-4142",
"cell":"(48) 5270-7303",
"id":{
"name":"",
"value":null
},
"picture":{
"large":"https://randomuser.me/api/portraits/men/40.jpg",
"medium":"https://randomuser.me/api/portraits/med/men/40.jpg",
"thumbnail":"https://randomuser.me/api/portraits/thumb/men/40.jpg"
},
"nat":"BR"
}
],
"info":{
"seed":"cb4b6bd1b687ebd0",
"results":1,
"page":1,
"version":"1.0"
}
}using System.Runtime.Serialization;
[DataContract]
public class RandomNameMe
{
[DataContract]
public class Rootobject
{
public Result[] results { get; set; }
public Info info { get; set; }
}
[DataContract]
public class Info
{
public string seed { get; set; }
public int results { get; set; }
public int page { get; set; }
public string version { get; set; }
}
[DataContract]
public class Result
{
public string gender { get; set; }
public Name name { get; set; }
public Location location { get; set; }
public string email { get; set; }
public Login login { get; set; }
public int registered { get; set; }
public int dob { get; set; }
public string phone { get; set; }
public string cell { get; set; }
public Id id { get; set; }
public Picture picture { get; set; }
public string nat { get; set; }
}
[DataContract]
public class Name
{
public string title { get; set; }
public string first { get; set; }
public string last { get; set; }
}
[DataContract]
public class Location
{
public string street { get; set; }
public string city { get; set; }
public string state { get; set; }
public int postcode { get; set; }
}
[DataContract]
public class Login
{
public string username { get; set; }
public string password { get; set; }
public string salt { get; set; }
public string md5 { get; set; }
public string sha1 { get; set; }
public string sha256 { get; set; }
}
[DataContract]
public class Id
{
public string name { get; set; }
public object value { get; set; }
}
[DataContract]
public class Picture
{
public string large { get; set; }
public string medium { get; set; }
public string thumbnail { get; set; }
}
}DataContractJsonSerializer json = new DataContractJsonSerializer(typeof(RandomNameMe));
string fileContent = textBox1.Text;
RandomNameMe person = (RandomNameMe)json.ReadObject(new System.IO.MemoryStream(Encoding.UTF8.GetBytes(textBox1.Text)));Решение задачи: «JSON получить нужные параметры»
textual
Листинг программы
using System.Collections.Generic;
namespace JSON01
{
public class RootObject
{
public List<Result> results { get; set; }
public Info info { get; set; }
}
public class Result
{
public string gender { get; set; }
public Name name { get; set; }
public Location location { get; set; }
public string email { get; set; }
public Login login { get; set; }
public int registered { get; set; }
public int dob { get; set; }
public string phone { get; set; }
public string cell { get; set; }
public Id id { get; set; }
public Picture picture { get; set; }
public string nat { get; set; }
}
public class Info
{
public string seed { get; set; }
public int results { get; set; }
public int page { get; set; }
public string version { get; set; }
}
#region Result
public class Name
{
public string title { get; set; }
public string first { get; set; }
public string last { get; set; }
}
public class Location
{
public string street { get; set; }
public string city { get; set; }
public string state { get; set; }
public int postcode { get; set; }
}
public class Login
{
public string username { get; set; }
public string password { get; set; }
public string salt { get; set; }
public string md5 { get; set; }
public string sha1 { get; set; }
public string sha256 { get; set; }
}
public class Id
{
public string name { get; set; }
public object value { get; set; }
}
public class Picture
{
public string large { get; set; }
public string medium { get; set; }
public string thumbnail { get; set; }
}
#endregion
}