0

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

5
  • this is not valid json. can you provie the valid json Commented Jun 10, 2020 at 5:20
  • I believe you think about that the option block not a valid one right?. I think that's the problem I have faced. I mean, we have received the Json as a string. So, we need to deserialze the same way. But the JsonConvertor through the error message. Commented Jun 10, 2020 at 5:24
  • Check the updated code Commented Jun 10, 2020 at 6:05
  • Take a look at this newtonsoft.com/json/help/html/SerializingJSON.htm And use this link to make you Json to Class dotnetfiddle.net/WJwvm3 Commented Jun 10, 2020 at 6:14
  • Does this answer your question? Deserialize nested JSON into C# objects Commented Jun 10, 2020 at 6:59

3 Answers 3

2

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; }
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your response vivek. I need to deserailze that option as a string. Because, the options property may changed. I mean, for example, we can received the two property, and some time we receive three. So, we are not able to define the class like above. I'm not sure I correct. But, this is my requirement. Sorry.
Ok... Thanks vivek. I will check with your idea and let you know. Thank you.
1

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('"'));

Comments

1

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; }
}

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.