2

How to json accept single value as array?

This json throw exception:

{ "code": "1", "message": "OK", "response": { "partners": { "id": "33", "name": "", "clienttypeid": "29", "logo": "", "description": "", "website": "www.site.com" } } }

This json parsed correct:

{ "code": "1", "message": "OK", "response": { "partners": [ { "id": "33", "name": "", "clienttypeid": "29", "logo": "", "description": "", "website": "www.site.com" }, { "id": "34", "name": "", "clienttypeid": "29", "logo": "", "description": "", "website": "www.site.com" } ] } }

Model:

public class Partner
{
    public string id { get; set; }
    public string name { get; set; }
    public string clienttypeid { get; set; }
    public string logo { get; set; }
    public string description { get; set; }
    public string website { get; set; }
}

public class Response
{
    public List<Partner> partners { get; set; }
}

public class RootObject
{
    public string code { get; set; }
    public string message { get; set; }
    public Response response { get; set; }
}

2 Answers 2

2

If you're trying to deserialize to a List, use array notation, even with one element

{
    "code":"1",
    "message":"OK",
    "response":{
        "partners":[
            {
                "id":"33",
                "name":"",
                "clienttypeid":"29",
                "logo":"",
                "description":"",
                "website":"www.site.com"
            }
        ]
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Server not returned array notation [...] if value is single
2

I used Json.net to convert the partners to array if it is a single object

string Normalize(string json)
{
    var jobj = JObject.Parse(json);
    if (!(jobj["response"]["partners"] is JArray))
    {
        jobj["response"]["partners"] = new JArray(jobj["response"]["partners"]);
    }
    return jobj.ToString();
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.