如何反序列化 Json 响应并将其存储到列表中?
How to deserialize the Json response and store it into List?
我是第一次开发网络服务应用程序。我有一个 http api,我用它来获取 URL 的列表以下载 Sample.xml 文件。我已经创建了一个包含 url 列表的对象 class,我正在尝试将 json 字符串直接反序列化到 URL 的列表中。知道为什么 DeserializeObject 方法不起作用吗?
这是我的代码:
public class StoreGetXmlUrl
{
public bool Flag{ get; set; }
public List<String> Urls { get; set; }
public string ErrorMessage { get; set; }
}
public static List<String> CheckForNewXmlFile(string storeKey)
{
StoreGetXmlUrl result = new StoreGetXmlUrl();
result.Status = true;
_logger.Info($"Fetching new file URL for storekey:{storeKey}");
try
{
var request = (HttpWebRequest)WebRequest.Create("https://execute-api.us-west-2.com/default/param?=" + storeKey);
var response = (HttpWebResponse)request.GetResponse();
var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
result.Urls = JsonConvert.DeserializeObject<List<String>>(responseString);
}
catch (WebException ex)
{
result.Status = false;
string errorMessage = ex.Message;
HttpWebResponse httpResponse = (HttpWebResponse)ex.Response;
using (WebResponse response = ex.Response)
using (Stream data = response.GetResponseStream())
using (StreamReader reader = new StreamReader(data))
{
errorMessage = reader.ReadToEnd();
}
#pragma warning disable 4014
_logger.Error($"Verification of store key failed with message: {errorMessage}");
#pragma warning restore 4014
result.ErrorMessage = errorMessage;
}
return result.Urls;
}
这是 json 响应的样子:
responseString = "{\"urls\": [\"https://uswest2.abc.com/sample.xml\"]}"
result.Urls = JsonConvert.DeserializeObject<List<String>>(responseString);
上面的行试图反序列化来自 json 的字符串数组,下面是一个 JSON 对象,该对象有一个名为 urls
的 属性 实际上是一个字符串数组:
"{\"urls\": [\"https://uswest2.abc.com/sample.xml\"]}"
所以你应该像这样反序列化:
result = JsonConvert.DeserializeObject<StoreGetXmlUrl>(responseString);
我是第一次开发网络服务应用程序。我有一个 http api,我用它来获取 URL 的列表以下载 Sample.xml 文件。我已经创建了一个包含 url 列表的对象 class,我正在尝试将 json 字符串直接反序列化到 URL 的列表中。知道为什么 DeserializeObject 方法不起作用吗?
这是我的代码:
public class StoreGetXmlUrl
{
public bool Flag{ get; set; }
public List<String> Urls { get; set; }
public string ErrorMessage { get; set; }
}
public static List<String> CheckForNewXmlFile(string storeKey)
{
StoreGetXmlUrl result = new StoreGetXmlUrl();
result.Status = true;
_logger.Info($"Fetching new file URL for storekey:{storeKey}");
try
{
var request = (HttpWebRequest)WebRequest.Create("https://execute-api.us-west-2.com/default/param?=" + storeKey);
var response = (HttpWebResponse)request.GetResponse();
var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
result.Urls = JsonConvert.DeserializeObject<List<String>>(responseString);
}
catch (WebException ex)
{
result.Status = false;
string errorMessage = ex.Message;
HttpWebResponse httpResponse = (HttpWebResponse)ex.Response;
using (WebResponse response = ex.Response)
using (Stream data = response.GetResponseStream())
using (StreamReader reader = new StreamReader(data))
{
errorMessage = reader.ReadToEnd();
}
#pragma warning disable 4014
_logger.Error($"Verification of store key failed with message: {errorMessage}");
#pragma warning restore 4014
result.ErrorMessage = errorMessage;
}
return result.Urls;
}
这是 json 响应的样子:
responseString = "{\"urls\": [\"https://uswest2.abc.com/sample.xml\"]}"
result.Urls = JsonConvert.DeserializeObject<List<String>>(responseString);
上面的行试图反序列化来自 json 的字符串数组,下面是一个 JSON 对象,该对象有一个名为 urls
的 属性 实际上是一个字符串数组:
"{\"urls\": [\"https://uswest2.abc.com/sample.xml\"]}"
所以你应该像这样反序列化:
result = JsonConvert.DeserializeObject<StoreGetXmlUrl>(responseString);