[英]make streamreader json file to object c#
I have code that makes an API call to a URL. 我有使API调用URL的代码。 Now i am stuck I need to get the JSON response written into T-SQL. 现在我陷入困境,我需要将JSON响应写入T-SQL。 I believe I will have to parse the response into C# objects. 我相信我将不得不将响应解析为C#对象。
The response contains information contained in my class lastKnownPosition. 响应包含我的类lastKnownPosition中包含的信息。
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
namespace apicall
{
public class Program
{
public static void Main(string[] args)
{
WebRequest request = WebRequest.Create("https://xxx/ion.json");
request.Method = "GET";
request.Headers["Authorization"] = "Basic " + Convert.ToBase64String(Encoding.Default.GetBytes("xxxxx:xxxxxx"));
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Console.WriteLine(response.StatusDescription);
Stream dataStream = response.GetResponseStream();
// Open the stream using a StreamReader for easy access.
StreamReader reader = new StreamReader(dataStream);
// Read the content.
string responseFromServer = reader.ReadToEnd();
var myUser = JsonConvert.DeserializeObject(responseFromServer);
// Display the content.
//Console.WriteLine(responseFromServer);
Console.WriteLine(myUser);
// Cleanup the streams and the response.
reader.Close();
dataStream.Close();
response.Close();
Console.ReadLine();
}
}
class lastKnownPosition
{
public string Desription { get; set; }
public string UnitId { get; set; }
public DateTime Localtimestamp { get; set; }
public float latitude { get; set; }
public float longitude { get; set; }
public string id { get; set; }
}
}
to parse to an specific object you have to set which object do you want, try next: 要解析为特定对象,您必须设置所需的对象,请尝试以下操作:
var myUser = JsonConvert.DeserializeObject<lastKnownPosition>(responseFromServer);
edit, if contains more than one 编辑,如果包含多个
var myUserList = JsonConvert.DeserializeObject<list<lastKnownPosition>>(responseFromServer);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.