C# 解析JSON流时遇到问题,
我能够使用C#Net中的基本身份验证和HTTPWebResponse连接到供应商的web API。我能够在StreamReader中获取JSON流,但在解析JSON时,我获取的是空值,无法获取单个字符串或变量中的数据元素。请参阅附加的JSON文件和帮助 这是我的密码C# 解析JSON流时遇到问题,,c#,asp.net,json,C#,Asp.net,Json,我能够使用C#Net中的基本身份验证和HTTPWebResponse连接到供应商的web API。我能够在StreamReader中获取JSON流,但在解析JSON时,我获取的是空值,无法获取单个字符串或变量中的数据元素。请参阅附加的JSON文件和帮助 这是我的密码 username = "****"; var password = "***"; var url = "*****"; var encoded = System.Convert.ToBase64String(System.T
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();
尝试了所有的解决方案,但遇到了麻烦。下面是我的一些JSON流
{“status”:“success”,“message”:“notes请求已成功处理。已返回100条记录。”,“data”:[{“noteId”:“0000”,“studentId”:“000000”,“advisorId”:“0000111”,“dateCreated”:“01/20/2015”,“studentStatus”:“On path”,“noteBody”:“这是一份测试笔记,针对特定的学生。它针对学生000000”,“编辑”:“N”,“日期编辑”:“N/A”,“privateNote”:“N”,“删除”:“N”,“删除日期”:“N/A”,“显示日期”:“2015年1月20日”,“noteCreatedWithReminder”:“N”,“noteCreatedWithStatusChange”:“N”},{“noteId”:“0001”,“studentId”:“000001”,“advisorId”:“0000111”,“dateCreated”:“2015年1月20日”,“studentStatus”:“进行中”,“noteBody”:”这是一份测试笔记,是为特定学生准备的。它是为学生000001准备的,“编辑的”:“N”,“日期编辑的”:“N/a”,“私人笔记”:“N”,“删除的”:“N”,“删除的日期”:“N/a”,“显示日期”:“01/20/2015”,“noteCreatedWithReminder”:“N”,“noteCreatedWithStatusChange”:“Y”}
我建议使用这样的库,以便更容易解析,您只需创建模型类:
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; }
}
公共课堂笔记
{
公共字符串noteId{get;set;}
公共字符串studentId{get;set;}
公共字符串advisorId{get;set;}
公共字符串dateCreated{get;set;}
公共字符串studentStatus{get;set;}
公共字符串注释体{get;set;}
已编辑的公共字符串{get;set;}
公共字符串dateEdited{get;set;}
公共字符串privateNote{get;set;}
已删除公共字符串{get;set;}
公共字符串removedDate{get;set;}
公共字符串displayDate{get;set;}
公共字符串NoteCreatedWithRemembers{get;set;}
公共字符串noteCreatedWithStatusChange{get;set;}
}
公众课堂反应
{
公共字符串状态{get;set;}
公共字符串消息{get;set;}
公共列表注释{get;set;}
}
那么这就是你解析它的方式
var response = JsonConvert.DeserializeObject<Response>(json);
Console.WriteLine("status =" + response.status);
var response=JsonConvert.DeserializeObject(json);
Console.WriteLine(“status=“+response.status”);
“无法获取单个字符串或变量中的数据元素。”--Hpw您正在提取这些单个字符串或变量吗?这很好!如何将列表(注释)绑定到GridView?抱歉,这里有新内容。