28879

Deserialize Json Object containing sub Array

Question:

I have the following Json string (containing subArray):

<blockquote>

{"attributes": [{"name":"a","data":["10","0","50"],"dataName":["2000248","2789290","2789291"], "dataValue":["a","b","c"],"toClick":"d"}, {"name":"v","data":["0","0","0","20"] ,"dataName": ["49500000","49500001","49500002","49500003"], "dataValue":["a","v","v","d"],"toClick":"d"}]}"

</blockquote>

I can't deserialize in csharp. [Updated after comment] What I did:

public class attributes { public string name { get; set; } public string[] data { get; set; } public string[] dataName { get; set; } public string[] dataValue { get; set; } public string toClick { get; set; } } public class DataJsonAttributeContainer { public List<attributes> JsonAttributeAfterSaves { get; set; } } public static T DeserializeFromJson<T>(string json) { T deserializedProduct = JsonConvert.DeserializeObject<T>(json, settings); return deserializedProduct; } private void testJson() { string JsonStr = "{\"attributes\":[{\"name\":\"a\",\"data\":[\"10\",\"0\",\"50\"],\"dataName\":[\"2000248\",\"2789290\",\"2789291\"],\"dataValue\":[\"a\",\"a\",\"d\"],\"toClick\":\"d\"},{\"name\":\"d\",\"data\":[\"0\",\"0\",\"0\",\"20\"],\"dataName\":[\"49500000\",\"49500001\",\"49500002\",\"49500003\"],\"dataValue\":[\"a\",\"a\",\"d\",\"d\"],\"toClick\":\"a\"}]}"; var container = DeserializeFromJson<DataJsonAttributeContainer>(JsonStr); }

Test method:

testJson()

What can I do?

Answer1:

Your property within DataJsonAttributeContainer has the wrong name given your JSON. Here's an example which works as far as I can tell:

using System; using System.Collections.Generic; using Newtonsoft.Json; public class Attributes { public string name { get; set; } public string[] data { get; set; } public string[] dataName { get; set; } public string[] dataValue { get; set; } public string toClick { get; set; } } public class DataJsonAttributeContainer { public List<Attributes> attributes { get; set; } } class Test { public static T DeserializeFromJson<T>(string json) { T deserializedProduct = JsonConvert.DeserializeObject<T>(json); return deserializedProduct; } static void Main() { string JsonStr = "{\"attributes\":[{\"name\":\"a\",\"data\":[\"10\",\"0\",\"50\"],\"dataName\":[\"2000248\",\"2789290\",\"2789291\"],\"dataValue\":[\"a\",\"a\",\"d\"],\"toClick\":\"d\"},{\"name\":\"d\",\"data\":[\"0\",\"0\",\"0\",\"20\"],\"dataName\":[\"49500000\",\"49500001\",\"49500002\",\"49500003\"],\"dataValue\":[\"a\",\"a\",\"d\",\"d\"],\"toClick\":\"a\"}]}"; var container = DeserializeFromJson<DataJsonAttributeContainer>(JsonStr); Console.WriteLine(container.attributes.Count); // Prints 2 Console.WriteLine(container.attributes[0].data.Length); // Prints 3 } }

Answer2:

You have to specify the correct types and, in the case of your DataJsonAttributeContainer, the correct property name too:

public class attributes { public string name { get; set; } public int[] data { get; set; } public int[] dataName { get; set; } public string[] dataValue { get; set; } public string toClick { get; set; } } public class DataJsonAttributeContainer { public List<attributes> attributes { get; set; } }

With the previous definitions, the following works for me (after I removed the last " from your JSON string):

var data = JsonConvert.DeserializeObject<DataJsonAttributeContainer>(jsonString);

Recommend

  • GWT Autobean Bean is null
  • MVC3 Razor - ListBox pre-select not working
  • how to adjust image in a panel in Java swing?
  • Linq Objects Group By & Sum
  • Spring Data JPA custom method causing PropertyReferenceException
  • Using $this when not in object context
  • Display issues when we change from one jquery mobile page to another in firefox
  • Dynamically accessing properties of knockoutjs observable array
  • req.body is undefined - nodejs
  • HTML download movie download link
  • AES padding and writing the ciphertext to a disk file
  • How to add date and time under each post in guestbook in google app engine
  • sending/ receiving email in Java
  • Change an a tag attribute in JavaScript based on screen width
  • How to show dropdown in excel using jrxml (jasper api)?
  • Convert array of 8 bytes to signed long in C++
  • Importing jscolor library in angular 2
  • Release, debug version and Authorization Google?
  • jquery mobile loadPage not working
  • Apache 2.4 - remove | delete | uninstall
  • How to delete a row from a dynamic generate table using jquery?
  • Python: how to group similar lists together in a list of lists?
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • Codeigniter doesn't let me update entry, because some fields must be unique
  • Proper way to use connect-multiparty with express.js?
  • Free memory of cv::Mat loaded using FileStorage API
  • Trying to get generic when generic is not available
  • Getting Messege Twice Using IMvxMessenger
  • Change div Background jquery
  • How to get Windows thread pool to call class member function?
  • Bitwise OR returns boolean when one of operands is nil
  • XCode 8, some methods disappeared ? ex: layoutAttributesClass() -> AnyClass
  • Checking variable from a different class in C#
  • Easiest way to encapsulate a HTML5 webpage into an android app?
  • Busy indicator not showing up in wpf window [duplicate]
  • costura.fody for a dll that references another dll
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?