我有以下json,当通过JSONLink运行时返回为有效的json并且我使用json2csharp创建了我的类。但我似乎无法获得我需要的数据,我已阅读
How to retrieve data from json data
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/
关于最后一个链接,一个数组:
数组以“[”开头,以“]结尾。价值观是分开的 用逗号。例如,
所以我已经阅读了所有内容,并且我的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所有类别genrerad请求----------
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
答案 0 :(得分:1)
您似乎正在反序列化为错误的对象。
您在上面发布的JSON实际上映射到var rootObjects = JsonConvert.DeserializeObject<RootObject[]>(jsonDoc);
类型的数组。
因此,您应该将其反序列化为:
CarouselItem
从那里,您可以通过访问其成员来获得必要的var singleRootObject = rootObjects[0];
var carouselItem = singleRootObject.value.value.carouselItem[0];
:
withFrame
答案 1 :(得分:0)
创建一个如下所示的包装类
public class WrapperEntity
{
public Value Value { get; set; }
}
然后反序列化为WrapperEntity,
var deserialized = JsonConvert.DeserializeObject<WrapperEntity>(sb);
var textToDisplay = deserialized.Value.value.carouselItem[0].textToDisplay;