[英]Deserialize JSON Array returned from HTTP-Response
I'm having trouble deserializing an JSON string to a class i Wrote. 我在将JSON字符串反序列化为我编写的类时遇到麻烦。
Here are my Classes 这是我的课程
class Newsletter
{
public string id;
public string state;
public string html;
public string name;
}
class ApiReply
{
int success;
//string value;
int status;
string reason;
}
class Newsletterlist : ApiReply
{
private const string URL = "https://www.newsletter2go.de/de/api/get/newsletters/";
public string key { private set; get; }
public Newsletterlist()
{
key = "MYAPIKEY";
HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(URL);
httpWebRequest.ContentType = "application/x-www-form-urlencoded";
httpWebRequest.Method = "POST";
byte[] data = PostData.get_postData(this);
httpWebRequest.ContentLength = data.Length;
using (var stream = httpWebRequest.GetRequestStream())
{
stream.Write(data, 0, data.Length);
}
var response = (HttpWebResponse)httpWebRequest.GetResponse();
var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
dynamic temp = JsonConvert.DeserializeObject(responseString);
}
public List<Newsletter> value {private set;get;}
}
I want to deserialize the JSON Return String into my Object Newsletterlist, but inside the JSON String, there's an JSON Array and I don't know how to deserialize the JSON Array to List Value. 我想反序列化JSON Return String到我的对象通讯列表中,但是在JSON字符串中,有一个JSON数组,而且我不知道如何反序列化JSON数组以列出值。
The JSON String looks something like this: JSON字符串如下所示:
{
success : 0,
value : [], <-- Value may contain a JSON Array wich I want to Serialize to List<Newsletter>
status :405,
reason : “Method Not Allowed , POST Required”
}
You need to know what is going to be returned to you in the array, if its just an array of string values then have value as a List<string>
if its a complex type then create an object that matches it and have a List<ComplexType>
您需要知道要在数组中返回的内容,如果它只是一个字符串值数组,那么它的值就是List<string>
如果它是复杂类型,则创建一个与之匹配并具有List<ComplexType>
class ApiReply
{
int success {get;set}
List<string> value {get;set;}
int status {get;set;}
string reason {get;set;}
}
or with complex type: 或具有复杂类型:
public class SomeType
{
public string Name {get;set;}
public int Age {get;set;}
}
class ApiReply
{
int success {get;set}
List<SomeType> value {get;set;}
int status {get;set;}
string reason {get;set;}
}
And the JSON for that would look like this: JSON如下所示:
{
success : 0,
value : [{name="fred", age=21},{name="paul", age=53}], <-- Value may contain a JSON Array
status :405,
reason : “Method Not Allowed , POST Required”
}
Example with Complex child array: JSONConvert.DeserializeObject not handling child array with unnamed array items 复杂子数组示例: JSONConvert.DeserializeObject不处理具有未命名数组项的子数组
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.