0

I have following JSON data, class and code but it doesn't working. I am getting List of 3 object but all are NULL. Can anybody please suggest me what I am missing here?

JSON data:

[
    {
        "donotpostalmail": {
            "Do Not Allow": {
                "Do Not Allow": 1
            },
            "Allow": {
                "Allow": 0
            }
        }
    },
    {
        "familystatuscode": {
            "Single": {
                "Single": 1
            },
            "Married": {
                "Married": 2
            },
            "Divorced": {
                "Divorced": 3
            },
            "Widowed": {
                "Widowed": 4
            }
        }
    },
    {
        "preferredcontactmethodcode": {
            "Any": {
                "Any": 1
            },
            "Email": {
                "Email": 2
            },
            "Phone": {
                "Phone": 3
            },
            "Fax": {
                "Fax": 4
            },
            "Mail": {
                "Mail": 5
            }
        }
    }
]

Class:

public class ResponseDataOfOptions
{
    public OptionsList mainList { get; set; }
}
public class OptionsList
{
    public Dictionary<string, Options> optionList { get; set; }
}
public class Options
{
    public Dictionary<string, int> options { get; set; }
}

.cs file Code:

List<ResponseDataOfOptions> optionList = JsonConvert.DeserializeObject<List<ResponseDataOfOptions>>(objResponse.ResponseDataOfOptions);
5
  • I believe your issue is mapping the response to a dictionary. Have you tried using something like json2csharp.com to build the classes? Admittedly I haven't worked with Json -> dict before, but that should be the issue. Commented Nov 16, 2016 at 10:02
  • I have tried. It has provided me really poor class for above JSON. So, I tried by my self. Usually, json2csharp.com doesn't provide better solution if JSON is based on Dictionary. Commented Nov 16, 2016 at 10:05
  • 3
    Your object model in no way represents your JSON data - how do you expect any deserializer to know where to put what values? Commented Nov 16, 2016 at 10:07
  • @Jamiec Can you please suggest Class which can represent above JSON? Commented Nov 16, 2016 at 10:10
  • 1
    @NanjiMange no, because there isnt one. Your only option is a hierarchy of dictionaries or a JObject as described in one of the answers. Commented Nov 16, 2016 at 10:12

3 Answers 3

2

You cannot use objects in this case because every object has different property names. The only way to deserialize this JSON is as follows.

 JsonConvert.DeserializeObject<List<
      Dictionary<string, 
          Dictionary<string, 
              Dictionary<string, int>
          >
      >
 >>(objResponse.ResponseDataOfOptions);

It is a list of dictionaries, in fact there are three levels of dictionaries. It's not a very friendly structure to work with data but that would do the deserialize.

You might also be interested in using some property detection using JObject and JToken and then inflate appropriate data transfer objects.

JObject list = JObject.Parse(objResponse.ResponseDataOfOptions);
foreach (var item in list) {
    JToken token = jobj["donotpostalmail"];
    if (token != null) {
        // inflate the corresponding data type.
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

I have created Dictionary in separate class same way you have represented here. I think I have tried this already. Let me try again.
You cannot use data transfer objects because of the different property names. Even though, there is a less neat way to parse JSON that might help you, I just added it to the answer.
I have used first method. It is working fine. It would be greatly helpful if class structure is possible. Well, this is greate solution. Thank you.
1

You cannot use an array that contains 3 different objects types. If you want to do this, I recommend to use a container object instead of an array:

{
    "responseDataOfOptions": {
        "donotpostalmail": {
            "Do Not Allow": {
                "Do Not Allow": 1
            },
            "Allow": {
                "Allow": 0
            }
        }
    },
    "optionsList": {
        "familystatuscode": {
            "Single": {
                "Single": 1
            },
            "Married": {
                "Married": 2
            },
            "Divorced": {
                "Divorced": 3
            },
            "Widowed": {
                "Widowed": 4
            }
        }
    },
    "options": {
        "preferredcontactmethodcode": {
            "Any": {
                "Any": 1
            },
            "Email": {
                "Email": 2
            },
            "Phone": {
                "Phone": 3
            },
            "Fax": {
                "Fax": 4
            },
            "Mail": {
                "Mail": 5
            }
        }
    }
}

The corresponding C# class must look like this:

public class JsonResponse {
    public ResponseDataOfOptions responseDataOfOptions{ get; set; }
    public OptionsList optionsList{ get; set; }
    public Options options { get; set; }
}

1 Comment

Thanks. It is one of the way if there is no solution. But I got solution.
0

please check your json string objResponse.ResponseDataOfOptions format whether is correct, maybe there are some &quot; in your string, if yes, you need to replace these &quot; as below:

List<ResponseDataOfOptions> optionList = JsonConvert.DeserializeObject<List<ResponseDataOfOptions>>(objResponse.ResponseDataOfOptions.Replace("&quot;", "\""));

1 Comment

The issue is the JSON string to a dictionary, rather than optionList being null.

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.