简体   繁体   中英

Having trouble parsing JSON stream,

I am able to connect to vendor's web API using basic authentication and HTTPWebResponse in C# .Net. I am able to get the JSON stream in the StreamReader but when parsing the JSON, I am getting null values and not being able to get data elements in individual strings or variables. Please see the attached JSON file and help...

Here is my code...

 username = "****";
 var password = "***";
 var url = "*****";
 var encoded = System.Convert.ToBase64String(System.Text.Encoding.GetEncoding("ISO-8859-1").GetBytes(username  + ":" + password));
 var webRequest = (HttpWebRequest)WebRequest.Create(url);
 webRequest.Headers.Add("Authorization", "Basic " + encoded);
 webRequest.Method = "GET";
 webRequest.Accept = "application/json";
 webRequest.ContentType = "application/json";


 var webResponse = (HttpWebResponse)webRequest.GetResponse();
 var reader = new StreamReader(webResponse.GetResponseStream());
 string s = reader.ReadToEnd();

Tried every solution out there but having trouble. Below is some of my JSON stream.

{"status":"success","message":"The request for notes has been processed successfully. 100 records have been returned.","data":[{"noteId":"0000","studentId":"000000","advisorId":"0000111","dateCreated":"01/20/2015","studentStatus":"On path","noteBody":"This is a test note, and it is for a particular student. It is for student 000000","edited":"N","dateEdited":"N/A","privateNote":"N","removed":"N","removedDate":"N/A","displayDate":"01/20/2015","noteCreatedWithReminder":"N","noteCreatedWithStatusChange":"N"},{"noteId":"0001","studentId":"000001","advisorId":"0000111","dateCreated":"01/20/2015","studentStatus":"In progress","noteBody":"This is a test note, and it is for a particular student. It is for student 000001","edited":"N","dateEdited":"N/A","privateNote":"N","removed":"N","removedDate":"N/A","displayDate":"01/20/2015","noteCreatedWithReminder":"N","noteCreatedWithStatusChange":"Y"}

I would recommend using libraries such as JSON.net it will make it easier to parse, all you need to do is create model classes:

public class Note
{
    public string noteId { get; set; }
    public string studentId { get; set; }
    public string advisorId { get; set; }
    public string dateCreated { get; set; }
    public string studentStatus { get; set; }
    public string noteBody { get; set; }
    public string edited { get; set; }
    public string dateEdited { get; set; }
    public string privateNote { get; set; }
    public string removed { get; set; }
    public string removedDate { get; set; }
    public string displayDate { get; set; }
    public string noteCreatedWithReminder { get; set; }
    public string noteCreatedWithStatusChange { get; set; }
}

public class Response
{
    public string status { get; set; }
    public string message { get; set; }
    public List<Note> notes { get; set; }
}

then this is how your parse it

var response = JsonConvert.DeserializeObject<Response>(json);
Console.WriteLine("status =" + response.status);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM