I have the following json, which when run through JSONLink comes back as valid json and I have created my classes using json2csharp. But I just cannot seem to get the data I need, I have read
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/
And regarding to the last link, an array:
An array begins with "[" and end with "]". And values are separated with commas. For example,
So with everything, I have read, and as my JSON is below, and I have followed the following steps.
Why do I get a null reference exception
Code:
[{
"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();
What am I missing here!!
-------------As Requested by All classes genererad by 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; }
}
--------------Full Object when converted to string --------
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
It seems you're deserializing into the wrong object.
The JSON you posted above actually maps to an array of type RootObject
.
So, you should deserialize it to such:
var rootObjects = JsonConvert.DeserializeObject<RootObject[]>(jsonDoc);
From there, you can get to the necessary CarouselItem
by accessing its members:
var singleRootObject = rootObjects[0];
var carouselItem = singleRootObject.value.value.carouselItem[0];