0

嗨,我有一个脚本,它使用 JSON 从名为 locations.xml 的文件中获取信息,现在我要做的是使用一些代码从数据库中获取信息。

下面是原始 JSON 文件和 XML 结构。

 //Check if the locations.xml file exists, if it does encode it to JSON for the app
if (file_exists('locations.xml')) {
    $xml = simplexml_load_file('locations.xml');
    $json = json_encode($xml);
    print_r($json);
} else {
    exit('Failed to open locations.xml.');
} 

 <location>
    <title>Union Station</title>
    <address>800 North Alameda Street Los Angeles, CA 90012</address>
    <details>Los Angeles Union Station (or LAUS, formerly the Los Angeles Union Passenger Terminal or LAUPT) is the main railway station in Los Angeles, California.</details>
    <images>
    </images>
    <marker>/css/img/markers/binoculars.png</marker>
    <lat>34.05515</lat>
    <lng>-118.23525</lng>
</location>

这给出了以下输出

  {"location":[{"title":"Hollywood Sign","address":"3204 Canyon Lake Drive, Los Angeles, CA","details":"The Hollywood Sign is a famous landmark in the Hollywood Hills area of Mount Lee, Santa Monica Mountains, in Los Angeles, California. The iconic sign spells out the name of the area in 45-foot-tall (14 m) and 350-foot-long (110 m) white letters.","images":{"image":["1_1.jpg","1_2.jpg"]},"marker":"\/css\/img\/markers\/photography.png","lat":"34.134103","lng":"-118.321694"}]}

我所做的是将上面的内容更改为调用 php 代码以从 MYSQL 数据库中获取相同的信息和结构,代码与 JSON 输出一起显示在下面。

    $query = mysql_query("SELECT * FROM locations");
     $rows = array();
     while($row = mysql_fetch_assoc($query)) {
     $rows['location'][] = $row;
     }
     print json_encode($rows);


     {"location":[{"Title":"Hollywood Sign","Address":"3204 Canyon Lake Drive, Los Angeles, CA","details":"The Hollywood Sign is a famous landmark in the Hollywood Hills area of Mount Lee, Santa Monica Mountains, in Los Angeles, California. The iconic sign spells out the name of the area in 45-foot-tall (1","images":"","marker":"\\\/css\\\/img\\\/markers\\\/photography.png","lat":"34.134103","lng":"-118.321694"}]}

正如您将看到的那样,两个输出是相同的,但是第二个没有显示信息,在我的代码中,我有以下代码似乎可以获取 JSON 数据并将其传递,

     //Register the Location Model
    Ext.regModel('Locations', {
    fields: locationFields //Set the fields to be stored from the config file

});

    //Load XML data from JSON into local store
    app.stores.LocationsList = new Ext.data.Store({
    model: "Locations", //Model to use for the Store
    sorters: [{
    property: 'title', //Set the title as a sorter to the listing card can use the   grouped list layout
                direction: 'ASC'
            }],
      proxy: {
      type: 'ajax', //Load JSON from our source defined in the config file
      url: JSON_SOURCE, 
      reader: {
        type: 'json',
        root: 'location'
        },
        id  : 'Locations'
      },
      getGroupString : function(record) {
    // return the first character of the address in order to group
    return record.get('title')[0];
   },

       listeners: {
      'load': function (t, r, s) {
        //Fire custom event once all the load is complete
         Ext.dispatch({
            controller: app.controllers.map,
            action: 'loaded',
            records: r
            });
          },
        },
      autoLoad : true //We start loading the info into the datastore immediately after the app is started to make it available ASAP

           });  

我已经通过这最后一段代码十几次,但不明白为什么它会以不同的方式处理第二批信息?

任何建议,将不胜感激。

谢谢

4

1 回答 1

0

解决了我在表格字段中添加了一个大写的第一个字母,一旦我改变了上面的代码就可以完美地工作。

于 2012-12-01T16:28:15.053 回答