0

JSon String is given below:

 {   
    "properties": {
    "jci": [
                {
                    "firstName": "Henk",
                    "lastName": "de Vries",
                    "Photo": "http://s3.amazonaws.com/mendeley-photos/6a/bd/6abdab776feb7a1fd4e5b4979ea9c5bfc754fd29.png",
                    "Title": "Dr.",
                    "Position": "Head of research group",
                    "Institution": "Vrije Universiteit Amsterdam",
                    "Fields_of_interest": ["medicine", "computer science"],
                    "emailAddress": "[email protected]",
                    "editorship": [
                        {
                            "title": "Dr.",
                            "editorial_role": "Editor in chief",
                            "date_start": 1460116283,
                            "date_end": 1460116283,
                            "publisher": {
                                "name": "Elsevier",
                                "role": "Project manager",
                                "emailAddress": "[email protected]"
                            }
                        }
                    ]
                }
            ],
            "sid": [
                {
                    "firstName": "Henk",
                    "lastName": "de Vries",
                    "Title": "Dr.",
                    "primary organisation": "Vrije Universiteit Amsterdam",
                    "emailAddress": "[email protected]",
                    "editorship": [
                        {
                            "title": "Dr.",
                            "editorial_role": "Editor in chief",
                            "publication_year": 2012
                        }
                    ]
                }
            ]
        }
    }

I have written code to deserialization

    JavaScriptSerializer ser = new JavaScriptSerializer();
                        JsonTest foo = new JsonTest();
                         foo = (JsonTest)ser.Deserialize<JsonTest>(JSONString);

public class JsonTest
    {
        public properties properties { get; set; }
    }
    public class properties
    {

        public List<jci> jci { get; set; }
        public List<sid>sid { get; set; }
    }

public class jci
    {
        public string firstName { get; set; }
        public string lastName { get; set; }
        public string Photo { get; set; }
        public string Position { get; set; }
        public string Institution { get; set; }
        public string emailAddress { get; set; }
        public string Title { get; set; }
        public List<string> Fields_of_interest { get; set; }
        public List<editorship> editorship { get; set; }

    }
    public class editorship
    {
        public string title { get; set; }
        public string editorial_role { get; set; }
        public string date_start { get; set; }
        public string date_end { get; set; }
        public Dictionary<string, string> publisher { get; set; }
    }

in this code there are 2 arrays of object called "editorship".. I have created "editorship" class for "jci".. how to access the values of editorship array of sid...?

1
  • 1
    have you tried create a class using json2csharp.com Commented Apr 27, 2016 at 6:58

1 Answer 1

2

Using json2csharp i generated these classes based on your JSON

public class Publisher
{
    public string name { get; set; }
    public string role { get; set; }
    public string emailAddress { get; set; }
}

public class Editorship
{
    public string title { get; set; }
    public string editorial_role { get; set; }
    public int date_start { get; set; }
    public int date_end { get; set; }
    public Publisher publisher { get; set; }
}

public class Jci
{
    public string firstName { get; set; }
    public string lastName { get; set; }
    public string Photo { get; set; }
    public string Title { get; set; }
    public string Position { get; set; }
    public string Institution { get; set; }
    public List<string> Fields_of_interest { get; set; }
    public string emailAddress { get; set; }
    public List<Editorship> editorship { get; set; }
}

public class Editorship2
{
    public string title { get; set; }
    public string editorial_role { get; set; }
    public int publication_year { get; set; }
}

public class Sid
{
    public string firstName { get; set; }
    public string lastName { get; set; }
    public string Title { get; set; }
    public string primary_organisation { get; set; }
    public string emailAddress { get; set; }
    public List<Editorship2> editorship { get; set; }
}

public class Properties
{
    public List<Jci> jci { get; set; }
    public List<Sid> sid { get; set; }
}

public class RootObject
{
    public Properties properties { get; set; }
}

You can then access your editorial values like this:

JavaScriptSerializer serializer = new JavaScriptSerializer();
var collection = serializer.Deserialize<RootObject>(jsonString);

foreach (var j in collection.properties.sid)
{
  Console.Write(j.editorship);
}

On a side note, you should consider using names that are more readable then jci and sid

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your answer

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.