.net - How to save json attribute in some c# variables -


this question has answer here:

i made code uses apis provided football-data.org, managed download json containing parameters expect receive. unless content in responsetext, divide content of responsetext in variables.

string requesturl = "http://api.football-data.org/alpha/soccerseasons/?season=2014"; httpwebrequest request = webrequest.create(requesturl) httpwebrequest; request.method = "get"; request.contenttype = "application/json";  string responsetext; using (httpwebresponse response = request.getresponse() httpwebresponse) using (var responsestream = new streamreader(response.getresponsestream())) {     responsetext = responsestream.readtoend(); }   console.writeline(responsetext); 

as can see structure of json shown in documentation follows:

example response:  { "_links": {    "self": { "href": "http://api.football-data.org/alpha/soccerseasons/354" },    "teams": { "href": "http://api.football-data.org/alpha/soccerseasons/teams" },    "fixtures": { "href": "http://api.football-data.org/alpha/soccerseasons/fixtures" },    "leaguetable": { "href": "http://api.football-data.org/alpha/soccerseasons/leaguetable" } },  "caption": "premier league 2014/15",  "league": "pl",  "year": "2014",  "numberofteams": 20,  "numberofgames": 380,  "lastupdated": "2014-12-21t10:47:43z" } 

i create variables once obtained content in responsetext, split of as:

string caption = caption of json (premier league 2014/15) string league = pl 

i not know if made clear idea , if code made good. relied on experience vb.net strong , i'm trying migrate c# purposes of study.

you can parse json object. structure (generated using json2csharp, edit bit there repetitive code):

public class self {     public string href { get; set; } }  public class teams {     public string href { get; set; } }  public class fixtures {     public string href { get; set; } }  public class leaguetable {     public string href { get; set; } }  public class links {     [jsonproperty("self")]     public self self { get; set; }     [jsonproperty("teams")]     public teams teams { get; set; }     [jsonproperty("fixtures")]     public fixtures fixtures { get; set; }     [jsonproperty("leaguetable")]     public leaguetable leaguetable { get; set; } }  public class rootobject {     [jsonproperty("_links")]     public links links { get; set; }     [jsonproperty("caption")]     public string caption { get; set; }     [jsonproperty("league")]     public string league { get; set; }     [jsonproperty("year")]     public string year { get; set; }     [jsonproperty("numberofteams")]     public int numberofteams { get; set; }     [jsonproperty("numberofgames")]     public int numberofgames { get; set; }     [jsonproperty("lastupdated")]     public string lastupdated { get; set; } } 

and after read content string, using serialization framework such json.net, parse object:

rootobject obj = jsonconvert.deserializeobject<rootobject>(responsetext); console.writeline(obj.caption); 

Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -