从 Json returns 中检索数据 null
Retrieving data from Json returns null
我有以下 json,当 运行 到 JSONLink 返回时有效 json 并且我创建了我的 类 使用 json2csharp。但是我似乎无法获得我需要的数据,我已经阅读
How to read data from json on C#
http://www.c-sharpcorner.com/article/working-with-json-string-in-C-Sharp/
http://www.c-sharpcorner.com/article/json-serialization-and-deserialization-in-c-sharp/
关于最后一个link,一个数组:
An array begins with "[" and end with "]". And values are separated
with commas. For example,
因此,所有内容,我都已阅读,正如我的 JSON 如下所示,我已按照以下步骤操作。
- 验证json
- 已创建类
- 转换为字符串
- 反序列化对象
为什么会出现空引用异常
代码:
[{
"value": {
"dtgeContentTypeAlias": "carousel",
"value": {
"name": "Layout",
"carouselItem": [{
"name": "Item 1",
"ncContentTypeAlias": "carouselItem",
"textToDisplay": "text to display",
"image": "umb://media/caa97c18a35f4fbbae2efa20f20c81ae",
"navigationLinks": [{
"id": "1063",
"name": "Home",
"udi": "umb://document/4dfc35a72aea4be5b3496d1c02a09072",
"url": "/",
"icon": "icon-document",
"published": true
}]
}]
},
"id": "59dc484a-1078-2447-34a3-c7ed5256cd48"
},
"editor": {
"name": "Layout",
"alias": "docType",
"view": "/App_Plugins/DocTypeGridEditor/Views/doctypegrideditor.html",
"render": "/App_Plugins/DocTypeGridEditor/Render/DocTypeGridEditor.cshtml",
"icon": "icon-item-arrangement",
"config": {
"allowedDocTypes": [],
"nameTemplate": "",
"enablePreview": true,
"viewPath": "/Views/Partials/Grid/Editors/DocTypeGridEditor/",
"previewViewPath": "/Views/Partials/Grid/Editors/DocTypeGridEditor/Previews/",
"previewCssFilePath": "",
"previewJsFilePath": ""
}
},
"active": false
}]
var jsonDoc = control.JObject.ToString();
var myDetails = JsonConvert.DeserializeObject<CarouselItem>(jsonDoc);
var test2 = JObject.Parse(jsonDoc).First.ToString();
string test = myDetails.textToDisplay.ToString();
我在这里错过了什么!!
------------应所有人的要求 类 由 Json2CSharp 生成------------
public class NavigationLink
{
public string id { get; set; }
public string name { get; set; }
public string udi { get; set; }
public string url { get; set; }
public string icon { get; set; }
public bool published { get; set; }
}
public class CarouselItem
{
public string name { get; set; }
public string ncContentTypeAlias { get; set; }
public string textToDisplay { get; set; }
public string image { get; set; }
public List<NavigationLink> navigationLinks { get; set; }
}
public class Value2
{
public string name { get; set; }
public List<CarouselItem> carouselItem { get; set; }
}
public class Value
{
public string dtgeContentTypeAlias { get; set; }
public Value2 value { get; set; }
public string id { get; set; }
}
public class Config
{
public List<object> allowedDocTypes { get; set; }
public string nameTemplate { get; set; }
public bool enablePreview { get; set; }
public string viewPath { get; set; }
public string previewViewPath { get; set; }
public string previewCssFilePath { get; set; }
public string previewJsFilePath { get; set; }
}
public class Editor
{
public string name { get; set; }
public string alias { get; set; }
public string view { get; set; }
public string render { get; set; }
public string icon { get; set; }
public Config config { get; set; }
}
public class RootObject
{
public Value value { get; set; }
public Editor editor { get; set; }
public bool active { get; set; }
}
----------------转换为字符串时的完整对象--------
jsonDoc "{\r\n \"value\": {\r\n \"dtgeContentTypeAlias\": \"carousel\",\r\n \"value\": {\r\n \"name\": \"Layout\",\r\n \"carouselItem\": [\r\n {\r\n \"name\": \"Item 1\",\r\n \"ncContentTypeAlias\": \"carouselItem\",\r\n \"textToDisplay\": \"text to display\",\r\n \"image\": \"umb://media/caa97c18a35f4fbbae2efa20f20c81ae\",\r\n \"navigationLinks\": [\r\n {\r\n \"id\": \"1063\",\r\n \"name\": \"Home\",\r\n \"udi\": \"umb://document/4dfc35a72aea4be5b3496d1c02a09072\",\r\n \"url\": \"/\",\r\n \"icon\": \"icon-document\",\r\n \"published\": true\r\n }\r\n ]\r\n }\r\n ]\r\n },\r\n \"id\": \"59dc484a-1078-2447-34a3-c7ed5256cd48\"\r\n },\r\n \"editor\": {\r\n \"name\": \"Layout\",\r\n \"alias\": \"docType\",\r\n \"view\": \"/App_Plugins/DocTypeGridEditor/Views/doctypegrideditor.html\",\r\n \"render\": \"/App_Plugins/DocTypeGridEditor/Render/DocTypeGridEditor.cshtml\",\r\n \"icon\": \"icon-item-arrangement\",\r\n \"config\": {\r\n \"allowedDocTypes\": [],\r\n \"nameTemplate\": \"\",\r\n \"enablePreview\": true,\r\n \"viewPath\": \"/Views/Partials/Grid/Editors/DocTypeGridEditor/\",\r\n \"previewViewPath\": \"/Views/Partials/Grid/Editors/DocTypeGridEditor/Previews/\",\r\n \"previewCssFilePath\": \"\",\r\n \"previewJsFilePath\": \"\"\r\n }\r\n },\r\n \"active\": false\r\n}" string
您似乎在反序列化为错误的对象。
您在上面发布的 JSON 实际上映射到类型 RootObject
的 数组 。
因此,您应该将其反序列化为:
var rootObjects = JsonConvert.DeserializeObject<RootObject[]>(jsonDoc);
从那里,您可以通过访问其成员来获得必要的 CarouselItem
:
var singleRootObject = rootObjects[0];
var carouselItem = singleRootObject.value.value.carouselItem[0];
像下面这样创建一个包装器class,
public class WrapperEntity
{
public Value Value { get; set; }
}
然后反序列化为那个WrapperEntity,
var deserialized = JsonConvert.DeserializeObject<WrapperEntity>(sb);
var textToDisplay = deserialized.Value.value.carouselItem[0].textToDisplay;
我有以下 json,当 运行 到 JSONLink 返回时有效 json 并且我创建了我的 类 使用 json2csharp。但是我似乎无法获得我需要的数据,我已经阅读
How to read data from json on C#
http://www.c-sharpcorner.com/article/working-with-json-string-in-C-Sharp/
http://www.c-sharpcorner.com/article/json-serialization-and-deserialization-in-c-sharp/
关于最后一个link,一个数组:
An array begins with "[" and end with "]". And values are separated with commas. For example,
因此,所有内容,我都已阅读,正如我的 JSON 如下所示,我已按照以下步骤操作。
- 验证json
- 已创建类
- 转换为字符串
- 反序列化对象
为什么会出现空引用异常
代码:
[{
"value": {
"dtgeContentTypeAlias": "carousel",
"value": {
"name": "Layout",
"carouselItem": [{
"name": "Item 1",
"ncContentTypeAlias": "carouselItem",
"textToDisplay": "text to display",
"image": "umb://media/caa97c18a35f4fbbae2efa20f20c81ae",
"navigationLinks": [{
"id": "1063",
"name": "Home",
"udi": "umb://document/4dfc35a72aea4be5b3496d1c02a09072",
"url": "/",
"icon": "icon-document",
"published": true
}]
}]
},
"id": "59dc484a-1078-2447-34a3-c7ed5256cd48"
},
"editor": {
"name": "Layout",
"alias": "docType",
"view": "/App_Plugins/DocTypeGridEditor/Views/doctypegrideditor.html",
"render": "/App_Plugins/DocTypeGridEditor/Render/DocTypeGridEditor.cshtml",
"icon": "icon-item-arrangement",
"config": {
"allowedDocTypes": [],
"nameTemplate": "",
"enablePreview": true,
"viewPath": "/Views/Partials/Grid/Editors/DocTypeGridEditor/",
"previewViewPath": "/Views/Partials/Grid/Editors/DocTypeGridEditor/Previews/",
"previewCssFilePath": "",
"previewJsFilePath": ""
}
},
"active": false
}]
var jsonDoc = control.JObject.ToString();
var myDetails = JsonConvert.DeserializeObject<CarouselItem>(jsonDoc);
var test2 = JObject.Parse(jsonDoc).First.ToString();
string test = myDetails.textToDisplay.ToString();
我在这里错过了什么!!
------------应所有人的要求 类 由 Json2CSharp 生成------------
public class NavigationLink
{
public string id { get; set; }
public string name { get; set; }
public string udi { get; set; }
public string url { get; set; }
public string icon { get; set; }
public bool published { get; set; }
}
public class CarouselItem
{
public string name { get; set; }
public string ncContentTypeAlias { get; set; }
public string textToDisplay { get; set; }
public string image { get; set; }
public List<NavigationLink> navigationLinks { get; set; }
}
public class Value2
{
public string name { get; set; }
public List<CarouselItem> carouselItem { get; set; }
}
public class Value
{
public string dtgeContentTypeAlias { get; set; }
public Value2 value { get; set; }
public string id { get; set; }
}
public class Config
{
public List<object> allowedDocTypes { get; set; }
public string nameTemplate { get; set; }
public bool enablePreview { get; set; }
public string viewPath { get; set; }
public string previewViewPath { get; set; }
public string previewCssFilePath { get; set; }
public string previewJsFilePath { get; set; }
}
public class Editor
{
public string name { get; set; }
public string alias { get; set; }
public string view { get; set; }
public string render { get; set; }
public string icon { get; set; }
public Config config { get; set; }
}
public class RootObject
{
public Value value { get; set; }
public Editor editor { get; set; }
public bool active { get; set; }
}
----------------转换为字符串时的完整对象--------
jsonDoc "{\r\n \"value\": {\r\n \"dtgeContentTypeAlias\": \"carousel\",\r\n \"value\": {\r\n \"name\": \"Layout\",\r\n \"carouselItem\": [\r\n {\r\n \"name\": \"Item 1\",\r\n \"ncContentTypeAlias\": \"carouselItem\",\r\n \"textToDisplay\": \"text to display\",\r\n \"image\": \"umb://media/caa97c18a35f4fbbae2efa20f20c81ae\",\r\n \"navigationLinks\": [\r\n {\r\n \"id\": \"1063\",\r\n \"name\": \"Home\",\r\n \"udi\": \"umb://document/4dfc35a72aea4be5b3496d1c02a09072\",\r\n \"url\": \"/\",\r\n \"icon\": \"icon-document\",\r\n \"published\": true\r\n }\r\n ]\r\n }\r\n ]\r\n },\r\n \"id\": \"59dc484a-1078-2447-34a3-c7ed5256cd48\"\r\n },\r\n \"editor\": {\r\n \"name\": \"Layout\",\r\n \"alias\": \"docType\",\r\n \"view\": \"/App_Plugins/DocTypeGridEditor/Views/doctypegrideditor.html\",\r\n \"render\": \"/App_Plugins/DocTypeGridEditor/Render/DocTypeGridEditor.cshtml\",\r\n \"icon\": \"icon-item-arrangement\",\r\n \"config\": {\r\n \"allowedDocTypes\": [],\r\n \"nameTemplate\": \"\",\r\n \"enablePreview\": true,\r\n \"viewPath\": \"/Views/Partials/Grid/Editors/DocTypeGridEditor/\",\r\n \"previewViewPath\": \"/Views/Partials/Grid/Editors/DocTypeGridEditor/Previews/\",\r\n \"previewCssFilePath\": \"\",\r\n \"previewJsFilePath\": \"\"\r\n }\r\n },\r\n \"active\": false\r\n}" string
您似乎在反序列化为错误的对象。
您在上面发布的 JSON 实际上映射到类型 RootObject
的 数组 。
因此,您应该将其反序列化为:
var rootObjects = JsonConvert.DeserializeObject<RootObject[]>(jsonDoc);
从那里,您可以通过访问其成员来获得必要的 CarouselItem
:
var singleRootObject = rootObjects[0];
var carouselItem = singleRootObject.value.value.carouselItem[0];
像下面这样创建一个包装器class,
public class WrapperEntity
{
public Value Value { get; set; }
}
然后反序列化为那个WrapperEntity,
var deserialized = JsonConvert.DeserializeObject<WrapperEntity>(sb);
var textToDisplay = deserialized.Value.value.carouselItem[0].textToDisplay;