8

This is driving me nuts... I am serializing a List to JSON using Json.net. I expect this JSON:

{
    "fieldsets": [
        {
            "properties": [
                {
                    "alias": "date",
                    "value": "2014-02-12T00:00:00"
                },
                {
                    "alias": "time",
                    "value": null
                }
            ],
            "alias": "eventDates",
            "disabled": false
        }
    ]
}

But instead I get this:

{
    "fieldsets": [
        {
            "properties": [
                {
                    "values": [
                        {
                            "alias": "date",
                            "value": "2014-07-13T00:00:00"
                        },
                        {
                            "alias": "time",
                            "value": "Registration begins at 8:00 AM; walk begins at 9:00 AM"
                        }
                    ]
                }
            ],
            "alias": "eventDates",
            "disabled": false
        }
    ]
}

The "values" collection I'd like as just a JSON array, but I can't for the life of me figure out how to get it to do this. I have a property on my "properties" objects called "values", so I understand why it's doing it, but I need just the straight array, not a JSON object.

2
  • 2
    Lets see the object definition for the object you're serializing. Commented Apr 22, 2015 at 21:01
  • 2
    json2csharp.com Go wild Commented Apr 22, 2015 at 21:02

3 Answers 3

11

For that response, you need this class structure

public class Property
{
    [JsonProperty("alias")]
    public string Alias { get; set; }

    [JsonProperty("value")]
    public string Value { get; set; }
}

public class Fieldset
{
    [JsonProperty("properties")]
    public Property[] Properties { get; set; }

    [JsonProperty("alias")]
    public string Alias { get; set; }

    [JsonProperty("disabled")]
    public bool Disabled { get; set; }
}

public class Response
{
    [JsonProperty("fieldsets")]
    public Fieldset[] Fieldsets { get; set; }
}
Sign up to request clarification or add additional context in comments.

3 Comments

This is very close except I'm getting back something strange on the "properties" array... It's outputting twice, once with "properties" and once with "Properties":
Nevermind... I had a JsonProperty attribute in the wrong place... This works! THANK YOU SO MUCH!!! Spent almost all day trying to get this right. :-/
Glad I was able to help :)
0

This may be the answer:

public class Property
{
    public string alias { get; set; }
    public string value { get; set; }
}

public class Fieldset
{
    public List<Property> properties { get; set; }
    public string alias { get; set; }
    public bool disabled { get; set; }
}

public class RootObject
{
    public List<Fieldset> fieldsets { get; set; }
}

2 Comments

Did you just post what json2csharp.com gave you?
Well, for one thing you can't use a default get/set on a List<t> but it's a start.
0

You can convert to JObject:

JObject jsonObject = JObject.Parse(myJsonString);

Navigation by key:

jsonObject["property"]["value"];//Property is value / object
jsonObject["value"];

Navigation by index:

jsonObject[0]["value"]; // Property is array / list
jsonObject[0];

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.