[英]I can't deserialize JSON object like I want
I have a web response like this: 我有这样的网络回复:
[{
"st1": [
"folio1",
"folio2"
],
"st2": [
"folio1"
]
}]
This is my code to call my web server: 这是调用我的Web服务器的代码:
var response = await client.GetAsync(uri+"server/getSTFolios/");
if (response.IsSuccessStatusCode)
{
var content = await response.Content.ReadAsStringAsync();
var Item = JsonConvert.DeserializeObject<List<ST>>(content);
return Item;
}
This is ST class, to deserialize my response 这是ST课,反序列化我的回答
public class ST
{
private string v;
public ST(string v)
{
this.v = v;
}
public string st { get; set; }
public string location { get; set; }
public string note { get; set; }
public int pk { get; set; }
public List<Folio> folios { get; set; }
public override string ToString()
{
return "ST: " + st;
}
}
But I can't obtain my object, I can't deserialize. 但是我无法获取对象,无法反序列化。 I obtaint this when I try print
Item.ToString()
我尝试打印
Item.ToString()
时获得了此信息
System.Collections.Generic.List`1[test2.ST]
I'm going to guess this is because your trying to deserialize your JSON object with 我猜这是因为您尝试使用以下方式反序列化JSON对象
var Item = JsonConvert.DeserializeObject<List<ST>>(content);
But the response you're receiving isn't in the same class format as ST
. 但是您收到的响应与
ST
类格式不同。
You're trying to convert an object that looks like 您正在尝试转换一个看起来像
{
"st1": ["folio1","folio2"],
"st2": ["folio1"]
}
to this 对此
public class ST
{
public string st { get; set; }
public string location { get; set; }
public string note { get; set; }
public int pk { get; set; }
public List<Folio> folios { get; set; }
}
They share no properties or anything at all. 他们没有任何财产或任何财产。 That's why, when debugging and hovering over
Item
, you do see a single object in that list, but all the values are null. 这就是为什么在调试和将鼠标悬停在
Item
,您确实会在该列表中看到一个对象,但是所有值都为null。 The JsonConvert.DeserializeObject
tried to convert your response into the format of ST
but found no matching properties, and simply created an empty ST
. JsonConvert.DeserializeObject
尝试将您的响应转换为ST
的格式,但是没有找到匹配的属性,只是创建了一个空的ST
。
If you were to receive a response with more than one object like 如果您收到包含多个对象的回复,例如
[{
"st1": ["folio1","folio2"],
"st2": ["folio1"]
},
{
"st1": ["folio1","folio2"],
"st2": ["folio1"]
}]
When hovering over Item
, you'll have 2 objects full of null values. 将鼠标悬停在
Item
,将有2个充满空值的对象。
You need to make your server respond with objects that look like the ST
class. 您需要使服务器使用类似于
ST
类的对象进行响应。 For example, if you had a response that looked something like this, I assume it would work: 例如,如果您的响应看起来像这样,我认为它会起作用:
[{
"st": "some string",
"location": "some string",
"note": "some string",
"pk": 123,
"folios" :
[{
"pk": 123,
"number": "some string"
},
{
"pk": 123,
"number": "some string"
}]
}]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.