3

I have the following json string

{
 "property1" : "value",
 "property2" : 2,
 "property3" : { "subprperty1" : "value" }
}

and I want to deserialize it (using Newtonsoft's Json.net) but keep property3 as a string.

So I have created the following model class

class JsonModel {
 string property1 {get; set;}
 int property2 {get; set;}
 string property3 {get; set;}
}

But when i deserialize it using JsonConvert.DeserializeObject<JsonModel>(json_string); I get the following error :

Newtonsoft.Json.JsonReaderException: 'Unexpected character encountered while parsing value: {. 

3 Answers 3

4
"property3" : { "subprperty1" : "value" }

This isn't nested json, its just a standard json object

Update

from your comments, i think you want a generic property. If your use case is you know before hand what is coming back, and there is some subset that is changing, generics might be where you should be

So you could just deserialize it in the standard way

class JsonModel<T> 
{
   string property1 {get; set;}
   int property2 {get; set;}
   T property3 {get; set;}
}

class SomeOtherMagicalClass 
{
   string subprperty1 {get; set;}
}

...

var results = JsonConvert.DeserializeObject<JsonModel<SomeOtherMagicalClass>>(json_string);
Sign up to request clarification or add additional context in comments.

3 Comments

Still, I need to keep prop3 as string, not another model "magical" class
Thats because the content/structure of prop3 is different for every call.
That's very interesting approach. Thanks , I will consider it.
4

Because property3 is an object instead of a string.

You can try to use a class to carry it.

public class Property3
{
    public string subprperty1 { get; set; }
}

public class JsonModel 
{
    public string property1 { get; set; }
    public int property2 { get; set; }
    public Property3 property3 { get; set; }
}

Note

There are two way can create model easily.

  1. You can use Web Essentials in Visual Studio, use Edit > Paste special > paste JSON as a class, you can easier to know the relation between Json and model.

  2. If you can't use Web Essentials you can instead of use http://json2csharp.com/ online JSON to Model class.

You can try to use those models to carry your JSON Format.

7 Comments

@TheGeneral Thanks your solution also work upvote too :)
Thank you, but I need to keep it as a string, not another model class.
You cannot keep it as string if it isn't one in the first place. But you could add a getter that serializes it to a string for you...
Thats because the content/structure of prop3 is different for every call.
But your property3 is a JSON object instead of a string. What's your mean about content/structure of prop3 is different for every call. ?
|
2

Well, if the task is to deserialize object while preserving property3 as json string we could do two things.

First: Parse the object using JObject.Parse :

class JsonModel {
    public string property1 { get; set; }
    public int property2 { get; set; }
    public string property3 { get; set; }
}

var json_string = "{ \"property1\" : \"value\", \"property2\" : 2, \"property3\" : { \"subprperty1\" : \"value\" } }";
var jObj = JObject.Parse(json_string);
var obj = new JsonModel()
{
    property1 = jObj["property1"].ToString(),
    property2 = (int) jObj["property2"],
    property3 = jObj["property3"].ToString(),
};
Console.WriteLine(obj.property3);

Second: deserialize the obj to dictionary of objects:

var dict = JsonConvert.DeserializeObject<Dictionary<string, object>>(json_string);
Console.WriteLine(dict["property3"].ToString());

Both output the same:

{ "subprperty1": "value" }

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.