且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在C#中将JSON转换为数据表

更新时间:2023-01-16 11:16:32

xxxx& depap = xxx& depdate = xxxxxx& dephr = xxxx);
httpWebRequest.Method = WebRequestMethods.Http.Get;
httpWebRequest.Accept = application / json; charset = utf-8;
string 文件;
var response =(HttpWebResponse)httpWebRequest.GetResponse();
使用 var sr = new StreamReader(response.GetResponseStream()))
{
file = sr.ReadToEnd();
}
// var json = JObject.Parse(file);
var table = JsonConvert.DeserializeAnonymousType(file, new {Makes = default ( DataTable)})。制作;
如果(table.Rows.Count > 0
{
// do一些
}





我尝试了什么:



我正在尝试执行此代码,但我得到例外 -



类型'Newtonsoft.Json.JsonReaderException异常'发生在Newtonsoft.Json.dll但未在用户代码中处理

附加信息:解析值时遇到意外字符:<。路径'',第0行,位置0.





所以kindely请帮我紧急


您的问题是您正在尝试将XML响应视为JSON(不确定您是否意识到它是xml是我为什么指出这一点)。



在浏览器中提取此URL:


xxxx& depap = XXX&安培; DEPDATE = XXXXXX&安培; dephr = XXXX> http://xml.flightview.com/fvDemoConsOOOI/fvxml.exe?a=fvxmldemoSoo1&b=thrk

HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create("http://xml.flightview.com/fvDemoConsOOOI/fvxml.exe?a=fvxmldemoSoo1&b=thrk$xxxx&depap=xxx&depdate=xxxxxx&dephr=xxxx");
httpWebRequest.Method = WebRequestMethods.Http.Get;
httpWebRequest.Accept = "application/json; charset=utf-8";
string file;
var response = (HttpWebResponse)httpWebRequest.GetResponse();
using (var sr = new StreamReader(response.GetResponseStream()))
{
file = sr.ReadToEnd();
}
//var json = JObject.Parse(file);
var table = JsonConvert.DeserializeAnonymousType(file, new { Makes = default(DataTable) }).Makes;
if (table.Rows.Count > 0)
{
//do something
}



What I have tried:

I am trying to do this code but i am getting exception -

An exception of type 'Newtonsoft.Json.JsonReaderException' occurred in Newtonsoft.Json.dll but was not handled in user code
Additional information: Unexpected character encountered while parsing value: <. Path '', line 0, position 0.


so kindely please help me urgent

xxxx&depap=xxx&depdate=xxxxxx&dephr=xxxx"); httpWebRequest.Method = WebRequestMethods.Http.Get; httpWebRequest.Accept = "application/json; charset=utf-8"; string file; var response = (HttpWebResponse)httpWebRequest.GetResponse(); using (var sr = new StreamReader(response.GetResponseStream())) { file = sr.ReadToEnd(); } //var json = JObject.Parse(file); var table = JsonConvert.DeserializeAnonymousType(file, new { Makes = default(DataTable) }).Makes; if (table.Rows.Count > 0) { //do something }



What I have tried:

I am trying to do this code but i am getting exception -

An exception of type 'Newtonsoft.Json.JsonReaderException' occurred in Newtonsoft.Json.dll but was not handled in user code
Additional information: Unexpected character encountered while parsing value: <. Path '', line 0, position 0.


so kindely please help me urgent


Your issue is that you are trying to treat an XML response as if it were JSON (not sure if you realized it was xml is why i point this out).

Pull up this URL in your browser:
http://xml.flightview.com/fvDemoConsOOOI/fvxml.exe?a=fvxmldemoSoo1&b=thrk