【发布时间】:2018-05-14 15:49:33
【问题描述】:
我有这个 JSON 数组作为输入。
[
{
"FirstName": "Test1",
"LastName": "Test2",
"Address": "London, GB",
"Error": "Something's gone wrong"
},
{
"FirstName": "Test3",
"LastName": "Test4",
"Address": "NewYork, US",
"Error": "Something's gone wrong"
},
{
"DisplayName": "ContactNumber",
"Value": "01234 123 123"
}
]
我想在 C# 中构建一个这样的 JSON 对象
[
"pages":{
"FirstName": "Test1",
"LastName": "Test2",
"Address": "London, GB",
"Error": "Something's gone wrong"
},
{
"FirstName": "Test3",
"LastName": "Test4",
"Address": "NewYork, US",
"Error": "Something's gone wrong"
},
"labels": {
"DisplayName": "ContactNumber",
"Value": "01234 123 123"
}
}
]
我已经创建了一个具有上述输出属性的模型,但是当我将对象反序列化为该模型时,它们没有被映射。所有值都返回 null。
var deserializedData = JsonConvert.DeserializeObject<List<Config>>(serializedData);
我检查时收到的响应是
[
{
"pages": null,
"labels": null
},
{
"pages": null,
"labels": null
}
]
谁能帮我在C#中为我想要的这种格式构建自定义模型。
提前致谢。
【问题讨论】:
-
您想要的 JSON 无效。如果您将其上传到jsonlint.com,您将获得
Error: Parse error on line 7: Expecting 'STRING', got '{'。"pages"和"labels"的值应该是数组吗? -
我已经创建了一个具有上述属性的模型,但是当我将对象反序列化到该模型中时,它们没有被映射。 -- 你能分享你到目前为止所尝试的吗?
-
尝试使用 Visual Studio 菜单并使用它从您的 json 创建类...您可能会遇到问题,因为它不会将 json 字符串反序列化到您的结构中
-
我想构建一个这样的 JSON 数组 [{ "Pages": { properties here}, "Labels": { properties here } }]
-
最好提供完整的 json 字符串,您输入的函数用于转换为对象 ...