【问题标题】:Parse json data categorywise in sencha touch在 sencha touch 中按类别解析 json 数据
【发布时间】:2013-03-29 14:22:28
【问题描述】:

对于我的 sencha touch 应用,我的 json 数据是这样的,

{
  "type": "FeatureCollection",
  "features": [
{
  "type": "Feature",
  "id": "rodos_business_pois.1",
  "geometry": {
    "type": "Point",
    "coordinates": [
      28.21962354993591,
      36.452844361147314
    ]
  },
  "geometry_name": "geom",
  "properties": {
    "id": null,
    "business_p": "AQUARIOUM",
    "name_owner": null,
    "surname_ow": null,
    "middle_nam": null,
    "category_n": "BAR",
    "category_c": "020200",
    "category_1": null,
    "category_2": null,
    "category_3": null,
    "address": "28 GRIVA GEORGIOU",
    "street_nam": "ÃÑÉÂÁ ÃÅÙÑÃÉÏÕ",
    "number": "28",
    "postcode": null,
    "places_en_": "RODOS",
    "f16": 1,
    "f17": 0,
    "tel": 2.24102778E9,
    "fax": 0,
    "mob": 0,
    "email": null,
    "url": null,
    "places_gr": null,
    "places_en": null
  }
},
{
  "type": "Feature",
  "id": "rodos_business_pois.2",
  "geometry": {
    "type": "Point",
    "coordinates": [
      28.225417523605692,
      36.436470953176716
    ]
  },
  "geometry_name": "geom",
  "properties": {
    "id": null,
    "business_p": "ZIGOS",
    "name_owner": null,
    "surname_ow": null,
    "middle_nam": null,
    "category_n": "BAR",
    "category_c": "020200",
    "category_1": null,
    "category_2": null,
    "category_3": null,
    "address": "89 KAZOULI STEFANOU",
    "street_nam": "ÊÁÆÏÕËÇ ÓÔÅÖÁÍÏÕ",
    "number": "89",
    "postcode": null,
    "places_en_": "RODOS",
    "f16": 1,
    "f17": 0,
    "tel": 2.24103987E9,
    "fax": 0,
    "mob": 0,
    "email": null,
    "url": null,
    "places_gr": null,
    "places_en": null
  }
},
{
  "type": "Feature",
  "id": "rodos_business_pois.3",
  "geometry": {
    "type": "Point",
    "coordinates": [
      28.223100266167258,
      36.45251556942936
    ]
  },
  "geometry_name": "geom",
  "properties": {
    "id": null,
    "business_p": "LA ROSA FASHION BAR",
    "name_owner": null,
    "surname_ow": null,
    "middle_nam": null,
    "category_n": "BAR",
    "category_c": "020200",
    "category_1": null,
    "category_2": null,
    "category_3": null,
    "address": "4 PAPANIKOLAOU GEORGIOU",
    "street_nam": "ÐÁÐÁÍÉÊÏËÁÏÕ ÃÅÙÑÃÉÏÕ",
    "number": "4",
    "postcode": null,
    "places_en_": "RODOS",
    "f16": 1,
    "f17": 0,
    "tel": 2.2410974E9,
    "fax": 0,
    "mob": 0,
    "email": null,
    "url": null,
    "places_gr": null,
    "places_en": null
  }
},
{
  "type": "Feature",
  "id": "rodos_business_pois.4",
  "geometry": {
    "type": "Point",
    "coordinates": [
      28.226588152300117,
      36.43403310824089
    ]
  },
  "geometry_name": "geom",
  "properties": {
    "id": null,
    "business_p": "G-NET",
    "name_owner": null,
    "surname_ow": null,
    "middle_nam": null,
    "category_n": "RESTAURANT",
    "category_c": "020100",
    "category_1": null,
    "category_2": null,
    "category_3": null,
    "address": "135 AGIAS ANASTASIAS",
    "street_nam": "ÁÃÉÁÓ ÁÍÁÓÔÁÓÉÁÓ",
    "number": "135",
    "postcode": null,
    "places_en_": "RODOS",
    "f16": 1,
    "f17": 0,
    "tel": 2.2410351E9,
    "fax": 0,
    "mob": 0,
    "email": null,
    "url": null,
    "places_gr": null,
    "places_en": null
  }
},
{
  "type": "Feature",
  "id": "rodos_business_pois.5",
  "geometry": {
    "type": "Point",
    "coordinates": [
      28.215719877096184,
      36.43079419827729
    ]
  },
  "geometry_name": "geom",
  "properties": {
    "id": null,
    "business_p": "ANDRMEDA LOUNGE BAR",
    "name_owner": null,
    "surname_ow": null,
    "middle_nam": null,
    "category_n": "CAFE",
    "category_c": "020100",
    "category_1": null,
    "category_2": null,
    "category_3": null,
    "address": "4 PAPANDREOU GEORGIOU",
    "street_nam": "ÐÁÐÁÍÄÑÅÏÕ ÃÅÙÑÃÉÏÕ",
    "number": "4",
    "postcode": null,
    "places_en_": "RODOS",
    "f16": 1,
    "f17": 0,
    "tel": 2.2410974E9,
    "fax": 2.2410975E9,
    "mob": 0,
    "email": null,
    "url": null,
    "places_gr": null,
    "places_en": null
  }
},
{
  "type": "Feature",
  "id": "rodos_business_pois.6",
  "geometry": {
    "type": "Point",
    "coordinates": [
      28.226209613062103,
      36.43806195312573
    ]
  },
  "geometry_name": "geom",
  "properties": {
    "id": null,
    "business_p": "DOLCE CAFE",
    "name_owner": null,
    "surname_ow": null,
    "middle_nam": null,
    "category_n": "RESTAURANT",
    "category_c": "020100",
    "category_1": null,
    "category_2": null,
    "category_3": null,
    "address": "14 DODEKANISIAKIS NEOLEAS",
    "street_nam": "ÄÙÄÅÊÁÍÇÓÉÁÊÇÓ ÍÅÏËÁÉÁÓ",
    "number": "14",
    "postcode": null,
    "places_en_": "RODOS",
    "f16": 1,
    "f17": 0,
    "tel": 2.24102988E9,
    "fax": 0,
    "mob": 0,
    "email": null,
    "url": null,
    "places_gr": null,
    "places_en": null
  }
},
.......
.......
....... and so on..

我可以使用 sencha touch json reader 一次解析所有这些数据, 但现在我想按类别解析这些数据,意味着 有一次,我想解析所有具有 BARCAFERESTAURANT 等类别的数据。

任何帮助都将得到满足。 提前致谢。

【问题讨论】:

  • 您必须阅读所有内容并对其进行过滤,或者修改 API 以便您传递特定类别并相应地获取 JSON
  • 你能推荐好的过滤器示例或关于如何修改 API 的建议吗?

标签: json parsing sencha-touch sencha-touch-2


【解决方案1】:

这就是您过滤商店的方式

store.filterBy(function(record, id){
    var cat = record.get('category_n');
    if(cat == 'BAR' || cat == 'CAFE' || cat == 'RESTAURANT'){
        return true;
    }else{
        return false;
    }
});

这将迭代每条记录并将其传递给函数,所以如果你想检查 category_n

编辑:确保在下次将数据加载到商店时清除商店过滤器

store.clearFilter();

【讨论】:

  • 谢谢..,在记录字段中我可以获取所有数据,但卡在那里,我无法继续。你能给我更多的解释吗?
  • Record 将包含来自您的 json 数组的元素,它将一个一个地迭代。从您的记录匹配您要过滤的条件。如果您返回 true,则该元素将在您的商店中可用如果你返回 false 那么它将从商店中删除...
  • 我无法获取它,因为它在使用 record.get('properties') 获取属性期间抛出错误。并抛出这样的错误,TypeError: 'undefined' is not an object (evalating 'prop.get')
  • 那更合理和预期,console.log(record);并检查属性是否在那里?我希望它应该有类型和功能
  • 你能否粘贴整个 json,以便找出你发布的 json 不完整,以及你在代理中使用的阅读器来阅读这个,可能我们必须放一个更多过滤器放在第一位
猜你喜欢
  • 1970-01-01
  • 2012-08-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多