3

How to deserialize the following JSON using JSON.NET:

{
"adjusted_amount":200.0,
"amount":2,
"uid":"admin",
"extra_params": {"uid":"admin","ip":"83.26.141.183","user_agent":"Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.52 Safari/537.36"}
}

I have the following code (but the problem is probably with 'extra_params' - it is not a string). I thought about creating new class for 'extra_params' but the problem is that the data in 'extra_params' may change.

I DONT NEED TO READ EXTRA_PARAMS AT ALL. All info I need, I get from the first 3 JSON variables.

My code:

public class RecData1
{
    public string uid { get; set; }
    public int amount { get; set; }
    public int adjusted_amount { get; set; }
    public string extra_params { get; set; }
}

var data = JsonConvert.DeserializeObject<RecData1>(payload);

where payload = string pasted in the first quotation

EDIT: The error I get now:

Error reading string. Unexpected token: StartObject. Path 'extra_params', line 1,     position 28.

at Newtonsoft.Json.JsonReader.ReadAsStringInternal()
at Newtonsoft.Json.JsonTextReader.ReadAsString()
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.ReadForType(JsonReader reader, JsonContract contract, Boolean hasConverter)
3
  • What errors do you get? Commented Jun 26, 2013 at 10:14
  • Now: Error reading string. Unexpected token: StartObject. Path 'extra_params', line 1, position 28. at Newtonsoft.Json.JsonReader.ReadAsStringInternal() at Newtonsoft.Json.JsonTextReader.ReadAsString() at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.ReadForType(JsonReader reader, JsonContract contract, Boolean hasConverter) Commented Jun 26, 2013 at 10:16
  • 1
    Are you sure your JSON is right? It could be an array of objects/strings rather than another JSON string. It cannot be the underscore, that is completely valid Commented Jun 26, 2013 at 10:20

4 Answers 4

1

First issue you have is that adjusted_amount is not an int, but a decimal (also problem with localization can occur there).

Secondly, you could in your class RecData1 remove string extra_params and that will fix it.

{
"adjusted_amount":200,
"amount":2,
"uid":"admin",
"extra_params": {"uid":"admin","ip":"83.26.141.183","user_agent":"Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.52 Safari/537.36"}
}

public class RecData1
{
    public string uid { get; set; }
    public int amount { get; set; }
    public int adjusted_amount { get; set; }
    //public string extra_params { get; set; }
}
Sign up to request clarification or add additional context in comments.

1 Comment

I just figured that out and I was on the point of sending my own answer. But you were first! (The problem with the INT)
1

You can have one more step forward by using dynamic:

 dynamic x = JsonConvert.DeserializeObject(payload);
 var data = new RecData1()
                 {
                     uid = x.uid,
                     amount = x.amount,
                     adjusted_amount = x.adjusted_amount
                 };

Comments

0

I believe the problem is because you extra param is not a string in your json.

It should be something like:

"extra_params": {"string........................................."}

It should work. Or you need to change your class to consider another object with all the properties you have in the json.

Comments

0

If you want to get extra_params, you can try this:

public class RecData1
{
    public string uid { get; set; }
    public int amount { get; set; }
    public int adjusted_amount { get; set; }
    public Dictionary<string,string> extra_params { get; set; }
}

var data = JsonConvert.DeserializeObject<RecData1>(payload);

Comments

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.