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

Vivek Nuna
  • 25,472
  • 25
  • 109
  • 197
Ramachandran
  • 103
  • 12
  • this is not valid json. can you provie the valid json – Vivek Nuna Jun 10 '20 at 05: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. – Ramachandran Jun 10 '20 at 05:24
  • Check the updated code – Vivek Nuna Jun 10 '20 at 06:05
  • Take a look at this https://www.newtonsoft.com/json/help/html/SerializingJSON.htm And use this link to make you Json to Class https://dotnetfiddle.net/WJwvm3 – Artavazd Jun 10 '20 at 06:14
  • Does this answer your question? [Deserialize nested JSON into C# objects](https://stackoverflow.com/questions/38793151/deserialize-nested-json-into-c-sharp-objects) – Pavel Anikhouski Jun 10 '20 at 06:59

3 Answers3

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; }
Vivek Nuna
  • 25,472
  • 25
  • 109
  • 197
  • 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. – Ramachandran Jun 10 '20 at 05:47
  • Ok... Thanks vivek. I will check with your idea and let you know. Thank you. – Ramachandran Jun 10 '20 at 06:19
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('"'));
Krishna Varma
  • 4,238
  • 2
  • 10
  • 25
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; }
}
shat90
  • 405
  • 3
  • 8