8

I'm having trouble trying to deserialize this JSON here:

{
    "response": {
        "numfound": 1,
        "start": 0,
        "docs": [
            {
                "enID": "9999",
                "startDate": "2013-09-25",
                "bName": "XXX",
                "pName": "YYY",
                "UName": [
                    "ZZZ"
                ],
                "agent": [
                    "BobVilla"
                ]
            }
        ]
    }
}

The classes I created for this are:

public class ResponseRoot {
    public Response response;
}

public class Response {
    public int numfound { get; set; }
    public int start { get; set; }
    public Docs[] docs;
}

public class Docs {
    public string enID { get; set; }
    public string startDate { get; set; }
    public string bName { get; set; }
    public string pName { get; set; }
    public UName[] UName;
    public Agent[] agent;
}

public class UName {
    public string uText { get; set; }
}

public class Agent {
    public string aText { get; set; }
}

But, whenever I call:

    ResponseRoot jsonResponse = sr.Deserialize<ResponseRoot>(jsonString);

jsonResponse ends up being null and the JSON isn't deserialized. I can't seem to tell why my classes may be wrong for this JSON.

5
  • In addition I'm getting this error: Cannot convert object of type 'System.String' to type 'UName' Commented Nov 21, 2013 at 22:32
  • 2
    Aren't the UName and agent members on Docs supposed to be arrays of strings? Commented Nov 21, 2013 at 22:33
  • uname and agent look like Lists of strings in the sample json. Commented Nov 21, 2013 at 22:34
  • 4
    Have you tried json2csharp.com ? Commented Nov 21, 2013 at 22:59
  • json2csharp.com is really g8 :) thanks @L.B Commented Dec 7, 2014 at 1:15

2 Answers 2

13

This should work for your classes, using json2csharp

public class Doc
{
    public string enID { get; set; }
    public string startDate { get; set; }
    public string bName { get; set; }
    public string pName { get; set; }
    public List<string> UName { get; set; }
    public List<string> agent { get; set; }
}

public class Response
{
    public int numfound { get; set; }
    public int start { get; set; }
    public List<Doc> docs { get; set; }
}

public class ResponseRoot
{
    public Response response { get; set; }
}
Sign up to request clarification or add additional context in comments.

1 Comment

No problem, it's very handy ;)
9

your code suggest that the UName Property of Docs is an array of objects, but it's an array of strings in json, same goes for agent

try this:

 public class Docs
 {
   public string enID { get; set; }
   public string startDate { get; set; }
   public string bName { get; set; }
   public string pName { get; set; }
   public string[]  UName;
   public string[] agent;
 }

and remove the UName and Agent classes

1 Comment

E.g., if UName were really the class specified, the JSON would have {uText: "ZZZ"} rather than just "ZZZ".

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.