【发布时间】:2018-02-22 12:58:38
【问题描述】:
我的应用中有这个类:
public class Control
{
public int Id { get; set; }
public string Name { get; set; }
public string ControlDescription { get; set; }
public string ControlExceptionText { get; set; }
public string Color { get; set; }
public double DataRetrieving_Frequency { get; set; }
public double delay_Factor { get; set; }
public Boolean Disabled { get; set; }
}
我的 json 字符串是:
var result =
"[
{\"id\":16,\"name\":\"trest\",\"controlDescription\":null,\"controlExceptionText\":null,\"color\":\"#abdb2c\",\"dataRetrieving_Frequency\":0.0,\"delay_Factor\":0.0,\"disabled\":false},
{\"id\":15,\"name\":\"test\",\"controlDescription\":null,\"controlExceptionText\":null,\"color\":\"#441fed\",\"dataRetrieving_Frequency\":1440.0,\"delay_Factor\":10.0,\"disabled\":false},
{\"id\":14,\"name\":\"Powershell\",\"controlDescription\":\"text. \",\"controlExceptionText\":\"exception.\",\"color\":\"#ad89d7\",\"dataRetrieving_Frequency\":1440.0,\"delay_Factor\":10.0,\"disabled\":false}
]"
我想反序列化这个 json 以适应我的Control class。所以我这样做是为了转换我的 Deserializeobject:
List<Control> test = JsonConvert.DeserializeObject<List<Control>>(result);
问题是test 返回null。
有什么想法吗?!
更新:
- 此代码在我的其他应用程序中运行良好,但在
Xamarin.Form应用程序中无法正常运行。 - 还尝试将
@附加到 JSON 字符串的开头,但出现此错误
解析值@时遇到意外字符。
更新:
我正在使用 Xamarin Live Player 审查我的应用程序,也许问题与此有关,因为许多开发人员在使用 Live Player 时抱怨序列化和反序列化错误。
【问题讨论】:
-
对我来说很好用。
-
Control 是 System.Web.UI 命名空间中的一个类。如果您在代码中包含该命名空间,则必须在 Control 类之前指定命名空间,例如列出
-
你确定是朋友? prnt.sc/iia1m0
-
使用完全相同的代码,我得到了结果。所以问题出在其他地方,你还没有提供。这是一个网络项目吗?您确定字符串格式正确吗?您可以尝试将
@添加到"[行并Ctrl + H'ing\"s 到""s 吗?因为这是我所做的唯一区别,所以您的示例不是有效字符串。 -
@SaadAli 您发布的用于分配给
result的文字代码不应编译,除非您将其设为一行。
标签: c# list json-deserialization