如何使用jquery检查json属性是否存在

时间:2014-04-01 10:40:55

标签: jquery json jquery-mobile

我有以下JSON结果,我试图提取并打印描述和标题详细信息,我想仅在标题和描述节点存在时打印详细信息,如何检查标题和描述节点是否存在并打印结果

Json数据:

"values": [
    {
      "isCommentable": true,
      "isLikable": true,
      "timestamp": 1395922690578,
      "updateContent": {
        "company": {
          "id": 123,
          "name": "XYZ"
        },
        "companyStatusUpdate": {"share": {
          "comment": "4 ",
          "content": {
            "description": " Public Services health benefit exchange takes a strategic sourcing approach to offer integrated and end-to-end insurance exchange solutions.",
            "title": "Public Sector"
          },
          "id": "s5854924124899520513",
          "source": {"serviceProvider": {"name": "LINKEDIN"}},
          "timestamp": 1395922690578,
          "visibility": {"code": "anyone"}
        }}
      },
      "updateKey": "UPDATE-c1283-5854924124777889792",
      "updateType": "CMPY"
    },
    {
      "isCommentable": true,
      "isLikable": true,
      "timestamp": 1395922642487,
      "updateContent": {
        "company": {
          "id": 123,
          "name": "XYZ"
        },
        "companyStatusUpdate": {"share": {
          "comment": "The interplay of socialmedia, social computing and social computation",
          "id": "s5854923923094790144",
          "source": {"serviceProvider": {"name": "LINKEDIN"}},
          "timestamp": 1395922642487,
          "visibility": {"code": "anyone"}
        }}
      },
      "updateKey": "UPDATE-c1283-5854923923065417729",
      "updateType": "CMPY"
    },
    {
      "isCommentable": true,
      "isLikable": true,
      "timestamp": 1395919454498,
      "updateContent": {
        "company": {
        "id": 123,
          "name": "XYZ"
        },
        "companyStatusUpdate": {"share": {
          "comment": "Prime ",
          "content": {
            "description": "Public Services, Inc. announced that it has been selected by Prime Therapeutics LLC to set up a testing center of excellence and unite application testing services delivery.",
             "title": "Newsroom"
          },
          "id": "s5854910551825604612",
          "source": {"serviceProvider": {"name": "LINKEDIN"}},
          "timestamp": 1395919454498,
          "visibility": {"code": "anyone"}
        }}
      },
      "updateKey": "UPDATE-c1283-5854910551674605568",
      "updateType": "CMPY"
    },
    {
      "isCommentable": true,
      "isLikable": true,
      "timestamp": 1395837571100,
      "updateContent": {
        "company": {
    "id": 123,
          "name": "XYZ"
        },
        "companyStatusUpdate": {"share": {
          "comment": "Gain an insight into the challenges faced to provide offline capabilities in Windows 8 apps http://infy.com/1o0BjC0",
          "id": "s5854567107836002304",
          "source": {"serviceProvider": {"name": "LINKEDIN"}},
          "timestamp": 1395837571100,
          "visibility": {"code": "anyone"}
        }}
      },
      "updateKey": "UPDATE-c1283-5854567107810832384",
      "updateType": "CMPY"
    }
    ]

Jquery代码:

 $.each(data.values, function (index, value) {
        $.each(value.updateContent.companyStatusUpdate, function (j, cs) {

            if (cs.content.hasOwnProperty("title")) {
        $("#list").append('<li><h1>' + cs.content.title + '</h1><p  id = "desc">' + cs.content.description + '</p></li>');

                $("#list").listview('refresh');

            }
        });
    });

0 个答案:

没有答案