0

i have following json.. i was convert my json into objectclass and get subjectid and class id loop through but its not working. i want subjectid and classid please help me how do this.i am new in c# please help me i am very thanful to you guys

[{
    "teacherid": 1,
    "teachername": "Addi Teacher",
    "class": {      
        "class_id": 2,      
        "class_name": "Class 9"
    },
    "subjecname": {

        "subject_id": 2,        
        "Subject_Name": "chemistry"
    },
    "$$hashKey": "object:10"
}, {
    "teacherid": 1,
    "teachername": "Addi Teacher",
    "class": {      
        "class_id": 2,      
        "class_name": "Class 9"
    },
    "subjecname": { 
        "subject_id": 4,        
        "Subject_Name": "Science"
    },
    "$$hashKey": "object:12"
}, {
    "teacherid": 1,
    "teachername": "Addi Teacher",
    "class": {

        "class_id": 2,      
        "class_name": "Class 9"
    },
    "subjecname": {

        "subject_id": 3,        
        "Subject_Name": "P.Study"
    },
    "$$hashKey": "object:14"
}]


//c# class
public class Class
{
    public int class_id { get; set; }
    public string class_name { get; set; }
}

public class Subjecname
{
    public int subject_id { get; set; }
    public string Subject_Name { get; set; }
}

public class RootObject
{
    public int teacherid { get; set; }
    public string teachername { get; set; }
    public Class @class { get; set; }
    public Subjecname subjecname { get; set; }
    public string __invalid_name__$$hashKey { get; set; }
}

// code
  subjectobject objsub = new JavaScriptSerializer().Deserialize<subjectobject>(jsonstring);

enter image description here

Addi Khan
  • 201
  • 2
  • 15
  • what is the problem you are having? Is the deserialization working and you just do not know how to get those properties? – CodingYoshi Apr 23 '17 at 21:12
  • how to get subject id becuase some time 6 subjectid some time 8 ..how to get subject id loop through – Addi Khan Apr 23 '17 at 21:13
  • 1
    If you are using NewtonSoft.Json then you need to write following code. `var obj = JsonConvert.DeserializeObject("your json");`. This will desrialize into array of RootObject class objects. – Chetan Apr 23 '17 at 21:54
  • please post as answer i will accept you thanks its working – Addi Khan Apr 23 '17 at 22:02

1 Answers1

0

Do the following steps to achieve what you need:

  1. Use my answer here to generate the C# classes for your JSON. The one you have created manually does not reflect your JSON accurately.
  2. Use NewtonSoft NuGet package to deserialize from json to C# type
  3. Then using Linq or a loop get the properties you need.

Here is what you r code will look like:

var obj = JsonConvert.DeserializeObject<Rootobject>("your json");
var result = obj.Property1.Select(x => 
    new { ClassId = x._class.class_id, SubjectId = x.subjecname.subject_id });

By the way, this is what my linked answer will generate for you:

public class Rootobject
{
    public Class1[] Property1 { get; set; }
}

public class Class1
{
    public int teacherid { get; set; }
    public string teachername { get; set; }
    public Class2 _class { get; set; }
    public Subjecname subjecname { get; set; }
    public string hashKey { get; set; }
}

public class Class2
{
    public int class_id { get; set; }
    public string class_name { get; set; }
}

public class Subjecname
{
    public int subject_id { get; set; }
    public string Subject_Name { get; set; }
}
Community
  • 1
  • 1
CodingYoshi
  • 25,467
  • 4
  • 62
  • 64
  • thanks for you help but its show error Additional information: Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'SMSystem.Rootobject' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly. – Addi Khan Apr 23 '17 at 21:44
  • @AddiKhan did you do all the steps as I outlined? – CodingYoshi Apr 23 '17 at 22:02
  • its working.....If you are using NewtonSoft.Json then you need to write following code. var obj = JsonConvert.DeserializeObject("your json");. This will desrialize into array of RootObject class objects. – – Addi Khan Apr 23 '17 at 22:11
  • Yes if you have an array then you will need the `[]`. Glad it is working. – CodingYoshi Apr 23 '17 at 22:20