5

I am trying to convert a JSON object in to C# array.

this is the JSon I Getfrom the Server webrequest response:

string result = sr.ReadToEnd(); // this line get me response 
result = {
    "subjects": [{
        "subject_id": 1,
        "subject_name": "test 1",
        "subject_class": 4,
        "subject_year": "2015",
        "subject_code": "t-1"
    },{
        "subject_id": 2,
        "subject_name": "test 2",
        "subject_class": 5,
        "subject_year": "",
        "subject_code": "t-2"
    }]
};

dynamic item = JsonConvert.DeserializeObject<object>(result);  

string iii = Convert.ToString(item["subjects"]);

I want to Get the Subjects and Save them in Array so i can use them for other purpose.

I use these to Method but always got the empty values.

List<subjects> subject1 = (List<subjects>)JsonConvert.DeserializeObject(iii, typeof(List<subjects>));

and

subjects[] subject2 = JsonConvert.DeserializeObject<subjects[]>(iii);

Please Help Me to Solve this.

And my Subject Class is..

class subjects
{
    public int id { get; set; }
    public string name { get; set; }
    public int class_name { get; set; }
    public string year { get; set; }
    public string code { get; set; }
}
phuzi
  • 12,078
  • 3
  • 26
  • 50
Awais
  • 169
  • 1
  • 2
  • 14

2 Answers2

2

You need to create a structure like this:

public class Subjects
{
    public List<Subject> subjects {get;set;}
}

public class Subject
{
    public string subject_id {get;set;}
    public string subject_name {get;set;}
}

Then you should be able to do:

Subjects subjects = JsonConvert.DeserializeObject<Subject>(result);
Ali Adlavaran
  • 3,697
  • 2
  • 23
  • 47
  • ... and if you then need to move data from this into some other data structure, e.g. an array, then you need to write logic to do that separately. JSON gets the value to you, and decoding that JSON gets a dynamic data-structure containing those values. What you *do* with the values is entirely up to you: JSON has done its part. – Mike Robinson Jul 03 '15 at 18:44
  • the answer provides strongly typed object, so you should declare object types before converting. i strongly recommend do it in this way. but you can convert json to `dynamic` type : take a look at this page : http://www.newtonsoft.com/json/help/html/QueryJsonDynamic.htm – Ali Adlavaran Jul 03 '15 at 18:54
  • Yes. I *agreed* with you. :-) – Mike Robinson Jul 03 '15 at 18:58
2

The property names won't match as is, because you subjects class don't have the 'subject_' prefix the JSON object has. Easiest fix is to change your property names as shown in Ali's answer. Just in case you need to keep your property names as is, you can also use a JsonProperty attribute to alter the serialization names (perhaps there's a more generic way using some sort of converter, but didn't think the amount of properties needed it)

    class subjects
    {
        [JsonProperty("subject_id")]
        public int id { get; set; }
        [JsonProperty("subject_name")]
        public string name { get; set; }
        [JsonProperty("subject_class")]
        public int class_name { get; set; }
        [JsonProperty("subject_year")]
        public string year { get; set; }
        [JsonProperty("subject_code")]
        public string code { get; set; }
    }

If you never need the root subjects you can also skip it without dynamic or an extra class with something like:

subjects[] arr = JObject.Parse(result)["subjects"].ToObject<subjects[]>();

(JObject is part of the namespace Newtonsoft.Json.Linq )

Me.Name
  • 12,259
  • 3
  • 31
  • 48