0

这是我从任何URL JSON文本http://www.example.com/json.php解析JSON在Qt的C++黑莓10

请任何一个可以告诉我,我该如何解析这个JSON并把它放在一个阵列,以便它可以是模式的定制化的ListView ?

让我告诉你,我是以前iOS开发者的黑莓开发新手。

我检查了这个link,但无法运行它。

如果任何人有任何完整的解决方案(例子或用法),请帮助我。

我从developer.blackberry.com下载了JSON的样本,但无法运行。

我还张贴在此link

{ 
    "status": "success", 
    "result": [ 
     { 
      "offer_id": "456", 
      "member_id": "648", 
      "offer_type": "printable", 
      "cat_name": "Health & Wellbeing", 
      "price": "50", 
      "discount": "20% Off.", 
      "title": "20% Off", 
      "quantity": "200", 
      "details": "Enjoy 20% Off any Service.", 
      "coupon_code": "45600010106", 
      "company_logo": "http://beta/files/offers/logos/", 
      "offer_image": "http://beta/files/offers/images/f4d118737e_image_456.jpg", 
      "bc_image": "http://beta/files/offers/qrcodes/qrcouponid_45600010106.png", 
      "company_address1": "Oud Metha - Mohammed Saeed Hareb Bldg. Opp. American Hospital", 
      "company_address2": "Not Available", 
      "company_city": "Not Available", 
      "company_phone": "04 357 6738 Mob: 509284567", 
      "location": "Oud Metha", 
      "company_name": "Golden House Gents Spa Club", 
      "merchant_name": "Golden House Gents Spa Club", 
      "url": "http://google.com", 
      "date_end": "2013/12/30", 
      "date_start": "2013/07/25", 
      "condition": "1. Cannot be Combined with any other offer.\r\n2. Advance booking required.\r\n3. This Voucher must be Mentioned during time of Booking.\r\n4. Not Valid on Thurs & Sat.\r\n5. Expires 31st December 2013.", 
      "rating": "0", 
      "latitude": "25.2374", 
      "longitude": "55.3117" 
     } 
    ] 
} 

回答

3

它的JsonDataAccess文档中的所有解释这个问题。你必须这样做:

// Create a data model with sorting keys for title 
GroupDataModel *model = 
    new GroupDataModel(QStringList() << "title"); 

// Load the JSON data 
JsonDataAccess jda; 
QVariant list = jda.load("yourfile.json")["result"].toVariantList(); 

// Add the data to the model 
model->insertList(list.value<QVariantList>()); 

// Add your model to a ListView 
listView->setDataModel(model); 
+1

如果我不想对数组进行排序。但我想显示来自Web服务的结果。我试过这个GroupDataModel * model = new GroupDataModel();但是它向我展示了相反的顺序 –

+1

如果顺序刚好颠倒过来而没有指定键,那么只需要颠倒sortedAscending #property-sortedascending)属性。 –

1

这是QML的方式,你不必碰任何C++代码。

首先您需要在QML文件中创建一个listview。

ListView { 
     id: listData 
     dataModel: ArrayDataModel { 
      id: theDataModel 
     } 
     .... 
} 

现在您将需要使用json文件中的数据填充listview。由于您正在对服务器进行调用,因此您需要发送请求。

  function sendRequest() { 
       console.log("DEBUG: sending request"); 
       var request = new XMLHttpRequest(); 
       request.onreadystatechange = function() { 
        // Need to wait for the DONE state or you'll get errors 
        if (request.readyState === XMLHttpRequest.DONE) { 
         if (request.status === 200) { 
          // if response is JSON you can parse it 
          var response = JSON.parse(request.responseText); 

           theDataModel.append({ 
            "offer_id": response.result.offer_id, 
            "member_id": response.result.member_id, 
            "offer_type": response.result.offer_type 
           }); 
          } 

        } else { 
          // Error 
          console.log("DEBUG: Status: " + request.status + ", Status Text: " + request.statusText);      
        } 
       } 
       // POST/GET request 
       request.open("GET", "http://www.example.com/json.php", true); 
       request.setRequestHeader("Accept", "application/json"); 
       request.send(); 
      } 

,并显示值,你可以使用StandardListItem

StandardListItem { 
        title: ListItemData.offer_id 
        description: ListItemData.offer_type 
        status: ListItemData.member_id 
       }