1

I am trying to Parse json Object below but i dont know how to do this Because Of "lecturer" in json Object ?? i dont know how i can manage "lecturer" their is another Object in Json But in data[0] its a object and at data[1] it is a flag or Boolean . this thing made me confused . Any Idea how i can achieve this ???

{
    "result": "Success",
    "data": [
        {
            "student_course_id": "82",
            "student_id": "30",
            "term_course_id": "18",
            "section_id": "3",
            "term_id": "12",
            "course_id": "15",
            "credit_hours": "26",
            "is_elective": "Elective",
            "is_practical": "0",
            "teacher_id": "10",
            "program_id": "5",
            "course_code": "E2",
            "course_title": "English 2",
            "lecturer": {
                "fname": "Ali",
                "lname": "farooq",
                "phone": "1234567890",
                "email": "[email protected]",
                "thumb": "../photos/thumb/1391515491.png"
            }
        },
        {
            "student_course_id": "83",
            "student_id": "30",
            "term_course_id": "19",
            "section_id": "3",
            "term_id": "12",
            "course_id": "16",
            "credit_hours": "26",
            "is_elective": "Elective",
            "is_practical": "0",
            "teacher_id": "8",
            "program_id": "5",
            "course_code": "C2",
            "course_title": "Culture 2",
            "lecturer": false
        }
        ]
}

C# Code

public async static Task<StudentSubjectsClassWithError> StudentSubjectsList()
{
    HttpClient client = new HttpClient();

    string baseUrl = getBaseUrl();

    try
    {
        string flickrResult = await client.GetStringAsync(baseUrl);
        StudentSubjectsClassWithError studentSubjectsResult = new StudentSubjectsClassWithError();
        try
        {

            StudentSubjectsJson apiData =
                JsonConvert.DeserializeObject<StudentSubjectsJson>(flickrResult);

            List<StudentSubjectsClass> mStudentSubjectsList = new List<StudentSubjectsClass>();

            if (apiData.data != null && apiData.result == "Success")
            {
                studentSubjectsResult.message = "";
                studentSubjectsResult.result = apiData.result;

                foreach (StudentSubjectsJsonItem data in apiData.data)
                {
                    StudentSubjectsClass studentSubjects = new StudentSubjectsClass();

                    studentSubjects.student_course_id = data.student_course_id;
                    studentSubjects.student_id = data.student_id;
                    studentSubjects.term_course_id = data.term_course_id;
                    studentSubjects.section_id = data.section_id;
                    studentSubjects.term_id = data.term_id;
                    studentSubjects.course_id = data.course_id;
                    studentSubjects.credit_hours = data.credit_hours;
                    studentSubjects.is_elective = data.is_elective;
                    studentSubjects.is_practical = data.is_practical;
                    studentSubjects.program_id = data.program_id;
                    studentSubjects.course_code = data.course_code;
                    studentSubjects.course_title = data.course_title;

                   //// studentSubjects.lecturer ?????

                    mStudentSubjectsList.Add(studentSubjects);
                }
            }
            else
            {
                studentSubjectsResult.result = apiData.result;
                studentSubjectsResult.message = "No records found.";

            }
            studentSubjectsResult.studentSubjectsList = mStudentSubjectsList;
            return studentSubjectsResult;
        }
        catch (JsonSerializationException)
        {

            try
            {

                StudentSubjectsErrorJson apiData =
                   JsonConvert.DeserializeObject<StudentSubjectsErrorJson>(flickrResult);
                studentSubjectsResult.result = apiData.result;
                studentSubjectsResult.message = apiData.data;
                studentSubjectsResult.studentSubjectsList = null;
                return studentSubjectsResult;
            }

            catch (JsonSerializationException)
            {
            }


        }
        return null;
    }
    catch (Exception)
    {
        return null;
     //   MessageBox.Show("Internet Connection Problem");
    }
}
4
  • I'm not sure why you need separate StudentSubjectsClass and StudentSubjectsJsonItem definitions with identical properties, but, have a look at AutoMapper which will save you lots of the property transfer work. Commented Feb 12, 2014 at 16:52
  • StudentSubjectsClass Actually Store and obj and StudentSubjectsJsonItem this one is used for mapping Commented Feb 12, 2014 at 17:31
  • Simple question, why you did not use FlickrNet ? flickrnet.codeplex.com Commented Feb 12, 2014 at 17:40
  • i will Look at this ... But now i have a Sol and its working . Thanks for your Concern Commented Feb 12, 2014 at 18:20

4 Answers 4

5

I am using Visual Studio 2012 and when I want to turn a Json string into a C# class object, I copy the Json string and

-> Click Edit
-> Click Paste Special
-> Click Paste JSON as Classes

or you can use the great online tool by Jonathan Keith http://json2csharp.com/

using json2csharp i got this class from your json

public class Datum
{
    public string student_course_id { get; set; }
    public string student_id { get; set; }
    public string term_course_id { get; set; }
    public string section_id { get; set; }
    public string term_id { get; set; }
    public string course_id { get; set; }
    public string credit_hours { get; set; }
    public string is_elective { get; set; }
    public string is_practical { get; set; }
    public string teacher_id { get; set; }
    public string program_id { get; set; }
    public string course_code { get; set; }
    public string course_title { get; set; }
    public object lecturer { get; set; }
}

public class RootObject
{
    public string result { get; set; }
    public List<Datum> data { get; set; }
}

Edit

I also noticed that your data array has the lecturer as an object, the second data has lecturer as a bool. You can fix this by simply not including lecturer if it doesn't exist. That would change the classes to this:

public class Lecturer
{
    public string fname { get; set; }
    public string lname { get; set; }
    public string phone { get; set; }
    public string email { get; set; }
    public string thumb { get; set; }
}

public class Datum
{
    public string student_course_id { get; set; }
    public string student_id { get; set; }
    public string term_course_id { get; set; }
    public string section_id { get; set; }
    public string term_id { get; set; }
    public string course_id { get; set; }
    public string credit_hours { get; set; }
    public string is_elective { get; set; }
    public string is_practical { get; set; }
    public string teacher_id { get; set; }
    public string program_id { get; set; }
    public string course_code { get; set; }
    public string course_title { get; set; }
    public Lecturer lecturer { get; set; }
}

public class RootObject
{
    public string result { get; set; }
    public List<Datum> data { get; set; }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Interesting that it didn't go into the lecturer object - just returned object
Its because in the first Datum, lecturer is an object, the second Datum lecturer is a bool.
0

You've got StudentSubjectsJson for the top level, and StudentSubjectsJsonItem for each isntance in the data array.

For the lecturer you need to define StudentSubjectsJsonLecturer and set is as a property of StudentSubjectsJsonItem called lecturer.

eg:

public class StudentSubjectsJsonItem {
    //Existing properties
    public StudentSubjectsJsonLecturer lecturer {get;set;}
}

public class StudentSubjectsJsonLecturer {
    public string fname {get;set;}
    public string lname {get;set;}
    //And so on...
}

Comments

0

Since you're already copying properties (you shouldn't have to, this is what deserialization is for) you can deserialize your data into a dynamic object (Newtonsoft Json v4+):

dynamic apiData = JsonConvert.DeserializeObject<StudentSubjectsJson>(flickrResult);

and proceed with the rest of the code as you have it, then when you have to deal with `lecturer' check if it's "false" as you normally would:

if (apiData.data[x].lecturer != false){
...
}

disclaimer: I haven't compiled this to try it, it's a suggestion what to try

Comments

0

I recommend you to use a third party json library instead of writing one yourself. Especially this one: click to download

It's a single-file json library.

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.