2

Part of the json read from Mailchimp looks like this.

    "interests": {
    "5e0344ae18": true,
    "545e5bdb01": true,
    "33aa542ea0": true,
    "f51a5f9716": true,
    "31109a4d58": true,
    "bf5f946fd4": true,
    "563320981e": false
}

So the properties used for deserialize should be this I believe.

       public Interests interests { get; set; }
       public class Interests
        {
            public bool 5e0344ae18 { get; set; }
            public bool 545e5bdb01 { get; set; }
            public bool 33aa542ea0 { get; set; }
            public bool f51a5f9716 { get; set; }
            public bool 31109a4d58 { get; set; }
            public bool bf5f946fd4 { get; set; }
            public bool 563320981e { get; set; }
        }

However the property names consisting of numbers and letters aren't valid with compile error for each like 'Invalid token '5e0344' in class, struct, or interface member declaration'. How can the property name match the name in the json data?

Richard
  • 21
  • 2

2 Answers2

1

While I don't believe you will be able to use property names beginning with numbers, you could possibly prefix these with a character or string and do some manual parsing.

Assuming you're working with C#, http://www.newtonsoft.com/json/help/html/t_newtonsoft_json_linq_jobject.htm may allow you to handle the JSON response and parse properties without using the automatic deserialize I'm guessing you're currently using.

Here's another post I found describing a similar problem and some potential solutions: Parse jsonObject when field names are unknowm

Community
  • 1
  • 1
Miek
  • 1,190
  • 7
  • 18
1

You can use Data Annotations to map your JSON properties to your Model properties
This works both ways (incoming/outgoing):

using Newtonsoft.Json; // or Json.Net (built-in)

[JsonProperty(PropertyName = "5e0344ae18")]
public bool YourPropertyName { get; set; }
Wouter Vanherck
  • 2,070
  • 3
  • 27
  • 41