2012-08-23 102 views
0

我有下面的json需要顯示在列表(locations數組)中。我可以通過rootProperty:response.data.locations獲取位置數組,但我如何獲得成功,徽標和backgroundpicture。任何建議都會有幫助。JSON解析問題sencha touch 2

Ext.data.JsonP.callback1(
{ 
    "response": { 
     "success": true, 
     "data": { 
      "logo": "http:\\/\\/www.websiteurl.nl\\/Images\\/logo.jpg", 
      "backgroundpicture": "http:\\/\\/www.websiteurl.nl\\/Images\\/logo.jpg", 
      "locations": [ 
       { 
        "id": "47", 
        "name": " 's-Gravenzande (De Carlton)", 
        "street": "Naaldwijkseweg", 
        "number": "257", 
        "zipcode": "2691 PV", 
        "city": "'s Gravenzande", 
        "province": "9", 
        "phone": "0174-412997", 
        "email": "[email protected]", 
        "kvk": "27210224", 
        "lat": "51.988593", 
        "longitude": "4.188087", 
        "slug": "-sGravenzande-De-Carlton", 
        "website": "http:\\/\\/www.websiteurl.nl", 
        "logo": null, 
        "backgroundpicture": null 
       }, 
       { 
        "id": "1", 
        "name": "Achterberg", 
        "street": "Weteringsteeg", 
        "number": "12", 
        "zipcode": "3911 VN", 
        "city": "Achterberg", 
        "province": "7", 
        "phone": "0317-613643", 
        "email": "[email protected]", 
        "kvk": "30093218", 
        "lat": "51.976316", 
        "longitude": "5.601611", 
        "slug": "-Achterberg", 
        "website": "http:\\/\\/www.websiteurl.nl", 
        "logo": null, 
        "backgroundpicture": null 
       } 


      ] 
     } 
    } 
} 
); 

如果我一個聽者添加到我的商店,然後登錄這些東西,然後我得到的值,但我要說的是我如何解析這個和存儲這在我的店裏

Ext.define("app.store.LocationStore",{ 
extend:'Ext.data.Store', 
id: 'locationStore', 

requires: 'Ext.data.proxy.JsonP', 

config:{ 
    autoLoad: 'true', 
    model:'app.model.LocationModel', 

    proxy: 
    { 
     type:'jsonp', 
     url:'http://www.websiteurl.nl/API/', 
     limitParam: false, 
     pageParam: false, 
     startParam: false, 
     extraParams: 
     { 
      method:'general', 
      token: '123456' 

     }, 
     reader:{ 
      type:'json', 
      successProperty: 'success', 
      rootProperty: 'response.data.locations' 
     } 
    }, 
    listeners:{ 
     load : function() 
     {console.log(this.getProxy().getReader().rawData.response.data.backgroundpicture); 
      console.log(this.getProxy().getReader().rawData.response.data.logo); 
      console.log(this.getProxy().getReader().rawData.response.success); 
      console.log("location store loaded"); 
     } 
    } 
} 
}); 
+0

什麼是你的'商店'或'模型'rootProperty設置? – AlexC

+0

在我的問題 – kush

回答

1

您需要使用hasOnehasMany關聯根據數據結構構建模型。

Ext.define('myApp.model.MyModel', { 
    extend: 'Ext.data.Model', 
    config: { 
     fields : [ 
      { 
       name : 'success' 
      } 
     ], 
     hasOne : [ 
      { 
       model   : 'myApp.model.Data, 
       name   : 'data', 
       associationKey : 'data' 
      } 
     ] 
    } 
}); 

Ext.define('myApp.model.Data, { 
    extend: 'Ext.data.Model', 
    config: { 
     fields : [ 
      { 
       name : 'logo' 
      }, 
      { 
       name : 'backgroundpicture' 
      } 
     ], 
     hasMany : [ 
      { 
       model   : 'myApp.model.Location', 
       name   : 'locations', 
       associationKey : 'locations' 
      } 
     ] 
    } 
}); 

Ext.define('myApp.model.Location', { 
    extend: 'Ext.data.Model', 
    config: { 
     fields : [ 
      { 
       name : 'id' 
      }, 
      { 
       name : 'name' 
      }, 
      { 
       name : 'street' 
      } 
    ...to be continued... 
+0

中添加了商店代碼,商店rootProperty變成了response.data對吧? – kush

+0

另外我如何獲得successProperty值? – kush

+0

修改了包含hasOne關聯的答案,以將位置模型值成功 –

1

您還可以使用簡單的JSONP請求來填充數據並將其添加到商店中。它會是這樣的:

Ext.data.JsonP.request({ 
    url:'http://www.websiteurl.nl/API/', 
    callbackKey: 'callback', 
    params: { 
     method:'general', 
     token: '123456' 
    }, 
    success: function(result, request) { 
      //result.response.success 
      //result.response.data.logo 
      //result.response.data.backgroundpicture 
      store.add(result.response.data.locations); 
    } 
}); 
+0

並將其添加到本地商店 – kush