我如何检查我的Json

时间:2014-01-30 12:46:57

标签: json titanium

我正在使用Titanium Appcelerator来开发Android应用程序..我在使用'ACS JSON'时遇到错误 ..我试图使用帖子查询,但我得到了不同种类的数组。 .i需要在访问之前检查字符串是否可用..

例如:

  "response": {
"posts": [
  {
    "id": "52e7800340b4b0aa134",
    "title": "test",
    "created_at": "2014-01-28T10:01:39+0000",
    "updated_at": "2014-01-30T11:59:54+0000",
    "content": "#hi all",
    "reviews_count": 3,
    "ratings_count": 3,
    "ratings_average": 3.67,
    "ratings_summary": {
      "5": 1,
      "3": 2
    },
    "user": {
      "id": "52e5e87f08a3e70b3309c3e3",
      "first_name": "aa",
      "last_name": "ss",
      "created_at": "2014-01-27T05:02:55+0000",
      "updated_at": "2014-01-30T11:58:49+0000",
      "external_accounts": [

      ],
      "confirmed_at": "2014-01-27T05:02:55+0000",
      "username": "ss",
      "role": "a",
      "admin": "false"
    },
    "custom_fields": {
      "postedby": "aa",
    }
  },
  {
    "id": "52e7908a3e70b3d0a9614",
    "title": "bb",
    "tags": [
      "sdf",
    ],
    "created_at": "2014-01-28T11:46:00+0000",
    "updated_at": "2014-01-30T11:09:17+0000",
    "content": "#hi@ #kWh v #sdf",
    "user": {
      "id": "52e5e87f08a3e70b3309c3e3",
      "first_name": "bb",
      "last_name": "bbc",
      "created_at": "2014-01-27T05:02:55+0000",
      "updated_at": "2014-01-30T11:58:49+0000",
      "external_accounts": [

      ],
      "confirmed_at": "2014-01-27T05:02:55+0000",
      "username": "b",
      "role": "b",
      "admin": "false"
    },
    "custom_fields": {
      "postedby": "b"
    }
  },

看到我在我的第一篇文章中获得* ratings_count *。虽然尝试访问此内部循环我收到错误..

现在我需要在访问之前检查字符串是否可用..!

2 个答案:

答案 0 :(得分:1)

在钛合金中,您可以使用if statment检查是否存在任何内容。您可以执行此操作

if(ratingcount){

//Do here whatever you want to do

}

由于

答案 1 :(得分:0)

尝试:

for (var i in response.posts) {
    post = response.posts[i];
    if (post.ratings_count) {
        /* Do operations for post with ratings */
    } else {
        /* Do operations for post without ratings */
    }
}

这是JavaScript中非常基本的操作,所以在深入了解Titanium之前,最好赶上普通的JS。