如何在asp.net C#

时间:2016-04-27 06:54:10

标签: c# asp.net json

JSon String如下:

 {   
    "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": "henk.de.vries@science.com",
                    "editorship": [
                        {
                            "title": "Dr.",
                            "editorial_role": "Editor in chief",
                            "date_start": 1460116283,
                            "date_end": 1460116283,
                            "publisher": {
                                "name": "Elsevier",
                                "role": "Project manager",
                                "emailAddress": "journal@elsevier.com"
                            }
                        }
                    ]
                }
            ],
            "sid": [
                {
                    "firstName": "Henk",
                    "lastName": "de Vries",
                    "Title": "Dr.",
                    "primary organisation": "Vrije Universiteit Amsterdam",
                    "emailAddress": "henk.de.vries@science.com",
                    "editorship": [
                        {
                            "title": "Dr.",
                            "editorial_role": "Editor in chief",
                            "publication_year": 2012
                        }
                    ]
                }
            ]
        }
    }

我编写了反序列化代码

    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; }
    }

在这段代码中有2个对象数组称为“editorship”..我为“jci”创建了“editorship”类。如何访问sid编辑数组的值...?

1 个答案:

答案 0 :(得分:2)

使用json2csharp我根据您的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; }
}

然后,您可以访问这样的编辑值:

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

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

另外,您应该考虑使用比 jci sid 更具可读性的名称

相关问题