6

I have the following JSON coming back from a remote API (I cannot modify the JSON returned)

{
    "APITicket": {
        "location": "SOMEVALUE",
        "ticket": "SOMEVALUE"
    }
}

Now using JSON.Net to convert to this to a model I have to create 2 models.

  public class TicketModel
    {
        public string location { get; set; }
        public string ticket { get; set; }
    }

    public class TicketContainer
    {
        public TicketModel APITicket { get; set; } 
    }

and do something like..

var myObject = JsonConvert.DeserializeObject<TicketContainer>(this.JSONResponse);

and this works well - my problem arises when I have around 50 calls to make to the API and really dont fancy creating a second 'Container' for each. Is there a way to bind the example above directly to the TicketModel?

2
  • If you need to deserialise in between every API call then there is really no way around this. You could batch deserialise after the 50 API calls have returned. Commented Jul 10, 2013 at 9:04
  • Hi @SamLeach I have 50 different API calls so it's not a quantity of calls but more just a pain in creating Container models that just are not useful for anything but the Deserialize! Commented Jul 10, 2013 at 9:06

3 Answers 3

1

You can do it this way:

var json = @"
            {
                'APITicket': {
                    'location': 'SOMEVALUE',
                    'ticket': 'SOMEVALUE'
                }
            }";

//Parse the JSON:
var jObject = JObject.Parse(json);

//Select the nested property (we expect only one):
var jProperty = (JProperty)jObject.Children().Single();

//Deserialize it's value to a TicketModel instance:
var ticket = jProperty.Value.ToObject<TicketModel>();
Sign up to request clarification or add additional context in comments.

Comments

1

use Newtonsoft's JArray to customize ur json before deserialize

public List<APITicket> JsonParser(string json)   
    {
        Newtonsoft.Json.Linq.JArray jArray = Newtonsoft.Json.Linq.JArray.Parse(json);

        var list = new List<APITicket>();

        foreach(var item in jArray)
        {
            list.Add(
                new APITicket { location = item["APITicket"]["location"],
                                ticket =   item["APITicket"]["ticket"]            
                }
            );
        }
        return list;
    }

Comments

0

Modify the JSON so it looks like this

{
    "location": "SOMEVALUE",
    "ticket": "SOMEVALUE"
}

and do

List<TicketModel> tickets = JsonConvert.DeserializeObject<List<TicketModel>>(this.JSONResponse);

or even

Dictionary<string, string> tickets = JsonConvert.DeserializeObject<Dictionary<string, string>>(this.JSONResponse);

so you don't need any models.

2 Comments

Hi Sam, I cant modify the JSON as it's an external API. - Updated question to clarify.
Why not? If it's returned Json, you can do what you want with it.

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.