1

I have a JSON string that I want to cut and put values into correct class. How do I do that in C#?

{
     \"First\":{\"FirstBool\":1, \"aString\":\"hello\"},
     \"Second\":{\"AnotherBool\":0,\"aString\":\"Hi again\"},
     \"Third\":{\"ThirdBool\":0,\"aString\":\"Hi for the 3rd time\", \"AdditionalString\":\"ADDITIONAL STRING\"}
}

This is how I've set up my class for taking JSON values.

public class First{

public static bool FirstBool {get; set;}
public static string aString {get; set;}

}

public class Second{

public static bool AnotherBool {get; set;}
public static string aString {get; set;}

}

public class Third{

public static bool ThirdBool {get; set;}
public static string aString {get; set;}
public static string AdditionalString {get; set;}

}
user7399041
  • 127
  • 1
  • 1
  • 9

3 Answers3

0

Your JSON string in invalid in First it was like "aString":"hello"" which should be "aString":"hello" So I hope and assumed it is a typo

You can install Newtonsoft JSON and then create class like

public class First
{
    [JsonProperty("FirstBool")]
    public int FirstBool { get; set; }

    [JsonProperty("aString")]
    public string aString { get; set; }
}

public class Second
{
    [JsonProperty("AnotherBool")]
    public int AnotherBool { get; set; }

    [JsonProperty("aString")]
    public string aString { get; set; }
}

public class Third
{
    [JsonProperty("ThirdBool")]
    public int ThirdBool { get; set; }

    [JsonProperty("aString")]
    public string aString { get; set; }

    [JsonProperty("AdditionalString")]
    public string AdditionalString { get; set; }
}

public class ClsResult
{
    [JsonProperty("First")]
    public First First { get; set; }

    [JsonProperty("Second")]
    public Second Second { get; set; }

    [JsonProperty("Third")]
    public Third Third { get; set; }
}

And Deserialize it like this

string jsonstr = File.ReadAllText(YourJSONFile);
var ser = JsonConvert.DeserializeObject<ClsResult>(jsonstr);
Community
  • 1
  • 1
Mohit S
  • 13,723
  • 6
  • 34
  • 69
0

First of all your class will look like

public class First {
    public int FirstBool { get; set; }
    public string aString { get; set; } 
}

public class Second {
    public int AnotherBool { get; set; }
    public string aString { get; set; } 
}

public class Third {
    public int ThirdBool { get; set; }
    public string aString { get; set; }
    public string AdditionalString { get; set; } 
}

public class Result {
    public First First { get; set; }
    public Second Second { get; set; }
    public Third Third { get; set; } 
}

and then you can Deserialize your json using JavaScriptSerializer

JavaScriptSerializer json_serializer = new JavaScriptSerializer();
Result objResult =  (Result)json_serializer.DeserializeObject("<your JSON>");
Pranav Patel
  • 1,541
  • 14
  • 28
0

If you are using visual studio, you could find out the class structure required for any json using Edit-> Paste Special -> Paste JSON as classes.

Create a root class which can be de-serialized to match your requirement.

public class Rootobject
{
    public First First { get; set; }
    public Second Second { get; set; }
    public Third Third { get; set; }
}

public class First
{
    public int FirstBool { get; set; }
    public string aString { get; set; }
}

public class Second
{
    public int AnotherBool { get; set; }
    public string aString { get; set; }
}

public class Third
{
    public int ThirdBool { get; set; }
    public string aString { get; set; }
    public string AdditionalString { get; set; }
}