Ramachandran
Ramachandran

Reputation: 103

Deserialize the Json property(Nested Json) as string in C#

I need requirement that the deserialize the json as string which is available inside another Json. I have the Json string as like below,

string testJson =
   "{
        "AssemblyName":"AssemplyName",
        "ClassName":"AssemplyName.ClassName",
        "Options":"{ "property1":"value1","property2":10}"
   }";

To deserialize, I have the class like below,

public class CType
{
    public string AssemblyName { get; set; }
    public string ClassName { get; set; }
    public string Options { get; set; }
}

So, I deserialize like below,

CType cType = JsonConvert.DeserializeObject<CType>(testJson);

Now, I expect the resuly like below,

AssemblyName = "AssemplyName"
ClassName = "AssemplyName.ClassName"
Options = "{ "property1":"value1","property2":10}"

It would be much appreciated anyone can help on this

Upvotes: 0

Views: 170

Answers (3)

shat90
shat90

Reputation: 405

Assuming you want the options deserialized as a string instead of a dictionary - you would need to change the json file and escape the quotes like below

{
"AssemblyName": "AssemplyName",
"ClassName": "AssemplyName.ClassName",
"Options": "
\"property1\":\"value1\",
\"property2\": 10   
"
}

With that done your existing code

 class Program
{
    static void Main(string[] args)
    {
        string testJson = File.ReadAllText(@"D:\test.json");

        CType cType = JsonConvert.DeserializeObject<CType>(testJson);

    }
}
public class CType
{
    public string AssemblyName { get; set; }
    public string ClassName { get; set; }
    public string Options { get; set; }
}

However if you want to make it to another object that is like a Dictionary - the json would need slight change as below

{
"AssemblyName": "AssemplyName",
"ClassName": "AssemplyName.ClassName",
"Options": {
"property1":"value1",
"property2": 10
}
}

The code would need the property to be changed to Dictionary

 class Program
{
    static void Main(string[] args)
    {
        string testJson = File.ReadAllText(@"D:\test.json");

        CType cType = JsonConvert.DeserializeObject<CType>(testJson);

    }
}
public class CType
{
    public string AssemblyName { get; set; }
    public string ClassName { get; set; }
    public Dictionary<string,string> Options { get; set; }
}

Upvotes: 1

Krishna Varma
Krishna Varma

Reputation: 4260

To read options form given json, you need to remove the additional quotations using Trim and DeserializeObject

var cType = JsonConvert.DeserializeObject<CType>(testJson);
var options = JsonConvert.DeserializeObject<Dictionary<string, string>>(cType.Options.Trim('"'));

Upvotes: 1

Vivek Nuna
Vivek Nuna

Reputation: 1

You can declare the class like this.

public class Options
{
    public string property1 { get; set; }
    public string value1 { get; set; }
    public int property2 { get; set; }
}

public class Example
{
    public string AssemblyName { get; set; }
    public string ClassName { get; set; }
    public Options Options { get; set; }
}

Then you caan deserialize and serilize the string like this.

string str = "json string";
Example cType = JsonConvert.DeserializeObject<Example>(str);
string json = JsonConvert.SerializeObject(cType.Options);

Valid Json:

{
    "AssemblyName": "AssemplyName",
    "ClassName": "AssemplyName.ClassName",
    "Options": {
        "property1 ": "",
        "value1": "",
        "property2": 10
    }
}

For dynamic nested json you can declare the Options as dictionary. Above code will work.

public Dictionary<string, string> Options { get; set; }

Upvotes: 2

Related Questions