09-20-2012 01:45 AM - edited 09-20-2012 01:47 AM
I want a simple example of inflating a listview with Json data.
Sample examples are not working
like we have a sample code
app.cpp
App::App()
{
Page *root = new Page;
ListView *listView = new ListView;
// Create the data model, specifying sorting keys of "firstName" and "lastName"
GroupDataModel *model = new GroupDataModel(QStringList() << "firstName"
<< "lastName");
// Create a JsonDataAccess object and load the .json file. The QDir::currentPath()
// function returns the current working directory for the app.
JsonDataAccess jda;
QVariant list = jda.load(QDir::currentPath() +
"/app/native/assets/employees.json");
// Insert the data into the data model. Because the root of the .json file is an
// array, a QVariant(QVariantList) is returned from load(). You can provide a
// QVariantList to a data model directly by using insertList().
model->insertList(list.value<QVariantList>());
qDebug()<<"the size of model is "<<model->size()<<"\n";
// Set the data model for the list view
listView->setDataModel(model);
// Set the content of the page and display it
root->setContent(listView);
Application::setScene(root);
}
with employees.json file in assets
[
{
"firstName" : "Mike",
"lastName" : "Chepesky"
"employeeNumber" : 01840192
},
{
"firstName" : "Westlee",
"lastName" : "Barichak"
"employeeNumber" : 47901927
},
{
"firstName" : "Jamie",
"lastName" : "Lambier"
"employeeNumber" : 51239657
},
{
"firstName" : "Denise",
"lastName" : "Marshall"
"employeeNumber" : 41239520
},
{
"firstName" : "Matthew",
"lastName" : "Taylor"
"employeeNumber" : 01963597
},
{
"firstName" : "Mark",
"lastName" : "Tiegs"
"employeeNumber" : 65321951
},
{
"firstName" : "Karla",
"lastName" : "Tetzel"
"employeeNumber" : 03266987
},
{
"firstName" : "Ian",
"lastName" : "Dundas"
"employeeNumber" : 29472012
},
{
"firstName" : "Marco",
"lastName" : "Cacciacarro"
"employeeNumber" : 56446691
}
]
On running this shows a blank screen with no list control.
Solved! Go to Solution.
09-20-2012 10:35 AM
you add file access in bar-descriptor.xml ?
09-21-2012 02:03 AM
Yes that permission is set in bar-descriptor.xml , bbdata library is also added in .pro file. Still no luck.
09-21-2012 06:16 PM
The issue may be that you have a line disconnect in your code for the following
QVariant list = jda.load(QDir::currentPath() + "/app/native/assets/employees.json");
This needs to be all on a single line as opposed to 2 lines. I made the same mistake the first time I copied and pasted the code. Let me know if that solves the issue
09-21-2012 08:35 PM
I found a simpler example that works right out of the box. It populates json from a file into a list view.
Just Create a New Cascades Project:

The IDE will populate a new project for you. I found this template very helpful.
![]()
Do people really want their phone making decisions for them? Whatever happened to the human element? Stop this madness!
09-22-2012 09:00 PM
You can also check out the stamp collector example - which populates from a JSON file. Here's the code from that example In that example I notice they set the parent for the data model and you didn't.
JsonDataAccess jda;
QVariantList mainList = jda.load("app/native/assets/stamps.json").value<QV ariantList>();
if (jda.hasError()) {
bb::data::DataAccessError error = jda.error();
qDebug() << "JSON loading error: " << error.errorType() << ": " << error.errorMessage();
return;
}
// A GroupDataModel is a helper class that the list uses for data handling.
// We sort on region in the model, this way will get different categories.
GroupDataModel *stampModel = new GroupDataModel(QStringList() << "region");
stampModel->setParent(this);
stampModel->insertList(mainList);
stampModel->setGrouping(ItemGrouping::ByFullValue) ;
stampList->setDataModel(stampModel);
09-25-2012 10:07 AM
Thanks all of you for your replies, actually problem was not in code. The problem was in employees.json file that I downloaded from developer's site .
[
{
"firstName" : "Mike",
"lastName" : "Chepesky" // after this ',' is missing
"employeeNumber" : 01840192
},
{
"firstName" : "Westlee",
"lastName" : "Barichak" // after this ',' is missing
"employeeNumber" : 47901927
},
{
"firstName" : "Jamie",
"lastName" : "Lambier" // after this ',' is missing
"employeeNumber" : 51239657
},
....................so on
]
means error occurred while parsing json. Very silly mistake. I would have checked it earlier.
So whoever is using this sample json file in their apps. Please correct it first and then use.
12-22-2012 02:46 PM
12-22-2012 06:03 PM
You shouldn't need anything to access a json file within that applications asset structure. If you want to access a json file on internet or shard files that you would select those in the application page.