1

I have the following Json string (containing subArray):

{"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"}]}"

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?

4
  • Why are you trying to deserialize the various properties to strings when they're arrays? Commented Apr 24, 2011 at 11:34
  • @JonSkeet but what I can do in order to get the property and the array? Commented Apr 24, 2011 at 11:37
  • @JohnJohnGa: Have you tried giving the properties appropriate types, e.g. string[] or int[]? Commented Apr 24, 2011 at 11:41
  • @JohnJohnGa: Please show a short but complete program with an attempt at the right types. Commented Apr 24, 2011 at 11:51

2 Answers 2

2

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
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.