Reputation: 73
I have this code that reads json. It works fine, but I cant figure out how to read the child node Name
under caller
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using Newtonsoft.Json;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create("https://**url**");
myReq.Method = "GET";
myReq.Headers.Add("Authorization: Basic **Authkey**");
WebResponse response = myReq.GetResponse();
Stream responseStream = response.GetResponseStream();
StreamReader reader = new StreamReader(responseStream);
string json = reader.ReadToEnd();
List<Incident> Incidents = JsonConvert.DeserializeObject<List<Incident>>(json);
foreach (Incident inc in Incidents)
{
LTInc.Text += inc.number + "</br>" + inc.briefDescription + "</br>" + inc.dynamicName + "</br></br>";
}
}
public class Incident
{
public string number;
public string briefDescription;
public string request;
//public IList<string> caller;
public string dynamicName;
}
}
The Json string looks somthing like this:
"number": "S 1901-079",
"request": "reg****",
"briefDescription": "sfdgfgfgfgfg",
"Caller":{
"ID": "1",
"Name": "TestName"
Upvotes: 0
Views: 350
Reputation: 3569
What about:
public class Incident
{
public string number;
public string briefDescription;
public string request;
public IList<Caller> caller;
public string dynamicName;
}
public class Caller
{
public int ID;
public string Name;
}
But: you should use properties instead of fields, and I suggets you switch to HttpClient
.
Two readings you should check:
Upvotes: 3
Reputation: 6866
You need to change your class to look like:
public class Incident
{
public string Number { get; set; }
public string Request { get; set; }
public string BriefDescription { get; set; }
public List<Caller> Caller { get; set; }
}
And introduce a new class called Caller
as:
public class Caller
{
public string ID { get; set; }
public string Name { get; set; }
}
Upvotes: 0