从json字符串中检索值

时间:2015-05-25 07:28:14

标签: c# json linq

我有 json 字符串。我想从json字符串中检索contact。以下json包含array个联系人。这是我的json字符串。

{
   "contacts": {
      "contact": [
         {
            "isConnection": false,
            "id": 33554611,
            "fields": [
               {
                  "id": 33554748,
                  "type": "name",
                  "value": {
                     "givenName": "Jhon",
                     "middleName": "",
                     "familyName": "Scot",
                     "prefix": "",
                     "suffix": "",
                     "givenNameSound": "",
                     "familyNameSound": ""
                  },
                  "editedBy": "OWNER",
                  "flags": [],
                  "categories": [],
                  "updated": "2012-12-23T07:40:23Z",
                  "created": "2012-12-23T07:40:23Z",
               },
               {
                  "id": 33554749,
                  "type": "email",
                  "value": "someone@example.com",
                  "editedBy": "OWNER",
                  "flags": [],
                  "categories": [],
                  "updated": "2012-12-23T07:40:23Z",
                  "created": "2012-12-23T07:40:23Z",
               }
            ]
         }
    }
}

在这里,我想检索givenName,familyName,email的值。如何从json字符串中检索这些值。

  

注意:json中有contact数组。我只发布了这个json的一个联系人。

我试过这样的事情。但没有奏效。

JObject json = JObject.Parse(returnStr);
JArray fields = (JArray)json["contacts"]["contact"]["fields"][0];
JArray FValues = (JArray)json["contact"]["fields"]["value"];

我试过这个

public class Field
    {
        public int id { get; set; }
        public string type { get; set; }
        public object value { get; set; }
        public string editedBy { get; set; }
        public List<object> flags { get; set; }
        public List<object> categories { get; set; }
        public string updated { get; set; }
        public string created { get; set; }
        public string uri { get; set; }
        public bool? isConnection { get; set; }
    }

    public class contact
    {
        public bool isConnection { get; set; }
        public int id { get; set; }
        public List<Field> fields { get; set; }
        public List<object> categories { get; set; }
        public int error { get; set; }
        public int restoredId { get; set; }
        public string created { get; set; }
        public string updated { get; set; }
        public string uri { get; set; }
    }

    public class Contacts
    {
        public List<contact> contact { get; set; }
        public int count { get; set; }
        public int start { get; set; }
        public int total { get; set; }
        public string uri { get; set; }
        public bool cache { get; set; }
    }

    public class RootObject
    {
        public Contacts contacts { get; set; }
    }

JavaScriptSerializer serializer1 = new JavaScriptSerializer();
RootObject obje = serializer1.Deserialize<RootObject>(returnStr);

但它在obje给我0值。

5 个答案:

答案 0 :(得分:3)

json对象的类(在纠正一些语法错误后用http://jsonutils.com/生成):

public class Field
{
    public int id { get; set; }
    public string type { get; set; }
    public object value { get; set; }
    public string editedBy { get; set; }
    public IList<object> flags { get; set; }
    public IList<object> categories { get; set; }
    public DateTime updated { get; set; }
    public DateTime created { get; set; }
}

public class Contact
{
    public bool isConnection { get; set; }
    public int id { get; set; }
    public IList<Field> fields { get; set; }
}

public class Contacts
{
    public IList<Contact> contact { get; set; }
}

public class Example
{
    public Contacts contacts { get; set; }
}

反序列化(您可能需要添加对System.Web.Extensions的引用):

System.Web.Script.Serialization.JavaScriptSerializer deSer = new System.Web.Script.Serialization.JavaScriptSerializer();
JSonPrintSettingsToXml.Input.Example deserializedJSON = deSer.Deserialize<JSonPrintSettingsToXml.Input.Example>(yourJSON);

这是更正后的JSON

{
    "contacts": {
        "contact": [
            {
                "isConnection": false,
                "id": 33554611,
                "fields": [
                    {
                        "id": 33554748,
                        "type": "name",
                        "value": {
                            "givenName": "Jhon",
                            "middleName": "",
                            "familyName": "Scot",
                            "prefix": "",
                            "suffix": "",
                            "givenNameSound": "",
                            "familyNameSound": ""
                        },
                        "editedBy": "OWNER",
                        "flags": [],
                        "categories": [],
                        "updated": "2012-12-23T07:40:23Z",
                        "created": "2012-12-23T07:40:23Z"
                    },
                    {
                        "id": 33554749,
                        "type": "email",
                        "value": "someone@example.com",
                        "editedBy": "OWNER",
                        "flags": [],
                        "categories": [],
                        "updated": "2012-12-23T07:40:23Z",
                        "created": "2012-12-23T07:40:23Z"
                    }
                ]
            }
        ]
    }
}

答案 1 :(得分:3)

  1. 首先使用jsonlint确认您的Json格式有效

  2. 然后使用json2csharp

    为其生成类库
    public class Field
    {
        public int id { get; set; }
        public string type { get; set; }
        public object value { get; set; }
        public string editedBy { get; set; }
        public List<object> flags { get; set; }
        public List<object> categories { get; set; }
        public string updated { get; set; }
        public string created { get; set; }
    }
    
    public class Contact
    {
        public bool isConnection { get; set; }
        public int id { get; set; }
        public List<Field> fields { get; set; }
    }
    
    public class Contacts
    {
        public List<Contact> contact { get; set; }
    }
    
    public class RootObject
    {
        public Contacts contacts { get; set; }
    }
    
  3. 使用Newtonsoft JSON将Json反序列化为对象,然后您可以只访问其属性值。

    JsonConvert.DeserializeObject<RootObject>(string json);
    

答案 2 :(得分:1)

您需要使用以下结构:

public class Contact
    {
        public bool isConnection { get; set; }
        public int id { get; set; }
        public List<Field> fields { get; set; }
    }
public class Field
{
    public int id { get; set; }
    public string type { get; set; }
    public object value { get; set; }
    public string editedBy { get; set; }
    public string[] flags { get; set; }
    public string[] categories { get; set; }
    public DateTime updated { get; set; }
    public DateTime created { get; set; }
}

public class Name
{
    public string givenName { get; set; }
    public string middleName { get; set; }
    public string familyName { get; set; }
    public string prefix { get; set; }
    public string suffix { get; set; }
    public string givenNameSound { get; set; }
    public string familyNameSound { get; set; }
}

然后反序列化它并使用LINQ来操作字段。

答案 3 :(得分:0)

如果您想坚持JObject而不是创建课程,请查看http://weblog.west-wind.com/posts/2012/Aug/30/Using-JSONNET-for-dynamic-JSON-parsing

中提供的示例

你必须列举这个

dynamic contacts = (JArray)json["contacts"]
foreach(dynamic contact in contacts.contact) {
   // look at the fields... 
}

PS。试一试,不要让VS对我这么无法验证确切的语法

答案 4 :(得分:0)

首先,我认为您会发现您的JSON格式不正确。在两个“创建”日期之后,您不需要额外的逗号,并且在第二个最后一个大括号之前缺少右方括号。我建议您始终使用这个令人敬畏的网站验证您的JSON:http://jsonformatter.curiousconcept.com

其次,您没有正确引用数组元素。虽然我同意@grundy你应该创建用于管理JSON和使用LINQ的类定义,但Newtonsoft库绝对没有任何问题。您仍然可以坚持使用您的方法。

试试这个: -

 <div id="container">
    <div id="div1">
        <div id="inner1"></div>
    </div>
    <div id="div2">
        <div id="inner2"></div>
    </div>
</div>