Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

Reply
Contributor
Posts: 11
Registered: ‎11-15-2013
My Device: blackbery 10

Loop over json file

I want to loop over json file data, and insert them to a table?

how can this be done?

 

right now i  have done this sample. which basically reads json file, and represents it.

 

// prepare the localization
    m_pTranslator = new QTranslator(this);
    m_pLocaleHandler = new LocaleHandler(this);

    bool res = QObject::connect(m_pLocaleHandler, SIGNAL(systemLanguageChanged()), this, SLOT(onSystemLanguageChanged()));
    // This is only available in Debug builds
    Q_ASSERT(res);
    // Since the variable is not used in the app, this is added to avoid a
    // compiler warning
    Q_UNUSED(res);

    // initial load
    onSystemLanguageChanged();

    Page *root = new Page;
    	ListView *listView = new ListView;
    	Container *topContainer = new Container;
    	Label *errorLabel = new Label;
    	// 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");
    

    	// Determine if an error occurred during the load() operation
    	if (jda.hasError()) {
    	    // Retrieve the error
    	    DataAccessError theError = jda.error();

    	    // Determine the type of error that occurred
    	    if (theError.errorType() == DataAccessErrorType::SourceNotFound)
    	        errorLabel->setText("Source not found: " + theError.errorMessage());
    	    else if (theError.errorType() == DataAccessErrorType::ConnectionFailure)
    	        errorLabel->setText("Connection failure: " + theError.errorMessage());
    	    else if (theError.errorType() == DataAccessErrorType::OperationFailure)
    	        errorLabel->setText("Operation failure: " + theError.errorMessage());
    	} else {
    	    errorLabel->setText("No error.");
    	}
    	// 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>());

    	// Set the data model for the list view
    	listView->setDataModel(model);

    	// Set the content of the page and display it
    	root->setContent(listView);
    	app->setScene(root);

 

 

 

 

 

 

employees.json

 

[
	{ 	
        "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
	}
]

 

 

 I get this page

 

http://s2.postimg.org/geczflr55/Untitled.png

 

I want to show firstname, lastname besides each other.And below them employee number.

In a table format.

Developer
Posts: 16,524
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport

Re: Loop over json file

looks to me as if the parsing runs correctly, you only have to access the correct properties in your ListItemComponent.
Maybe you can post your ListView code?
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Contributor
Posts: 11
Registered: ‎11-15-2013
My Device: blackbery 10

Re: Loop over json file

what listview code?

 

I only have one qml file which is this

 

/*
 * Copyright (c) 2011-2013 BlackBerry Limited.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import bb.cascades 1.2

Page {
    Container {
        //Todo: fill me with QML
        Label {
            // Localized text with the dynamic translation and locale updates support
            text: qsTr("Hello World") + Retranslate.onLocaleOrLanguageChanged
            textStyle.base: SystemDefaults.TextStyles.BigText
        }
    }
}

 

And I only have main.cpp where I call that function to read and write json file to qml.

There isnt anything more to it.How can I loop over the json file, insert to table? Or even make a row clickable inside the table?

Contributor
Posts: 11
Registered: ‎11-15-2013
My Device: blackbery 10

Re: Loop over json file

I have included this

#include <bb/cascades/Application>
#include <bb/cascades/QmlDocument>
#include <bb/cascades/AbstractPane>
#include <bb/cascades/LocaleHandler>
#include <bb/cascades/ListView>
#include <bb/cascades/AbstractPane>
#include <bb/cascades/LocaleHandler>
#include <bb/cascades/GroupDataModel>
#include <bb/cascades/Label>
#include <bb/cascades/Container>
#include <bb/cascades/Page>
#include <bb/data/JsonDataAccess>
using namespace bb::cascades;


I haven't written listview class
Developer
Posts: 16,524
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport

Re: Loop over json file

I see, you have the ListView in c++.
You can define ListItemComponents in c++ as well, but i am not sure how to bind the properties there. I think the cookbook sample has a c++ implementation, maybe it helps.

In any case, you have to tell your ListView what attributes should be displayed.
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Contributor
Posts: 11
Registered: ‎11-15-2013
My Device: blackbery 10

Re: Loop over json file

can please tell me how can I loop over the json file?
the qvariant list?

Contributor
Posts: 11
Registered: ‎11-15-2013
My Device: blackbery 10

Re: Loop over json file

[ Edited ]

also the cookbook example has no listview

 

 

edit: it has this

 

ListView *CascadesCookbookApp::createRecipeListView() {
	// The list view and its item manager (where item are created and set up)
	ListView *recipeListView = new ListView();
	RecipeItemFactory *recipeItemManager = new RecipeItemFactory();

	// The list model, item data is added to a variant map.
	QVariantMap map = QVariantMap();
	map["title"] = QString("Introduction");
	map["image"] = QString("assets/images/items/pear_peach.png");
	mRecipeModel << map;
	map["title"] = QString("Image");
	map["image"] = QString("assets/images/items/peach.png");
	mRecipeModel << map;
	map["title"] = QString("Nine Slice");
	map["image"] = QString("assets/images/items/lasagna.png");
	mRecipeModel << map;
	map["title"] = QString("Button");
	map["image"] = QString("assets/images/items/button.png");
	mRecipeModel << map;
	map["title"] = QString("Slider");
	map["image"] = QString("assets/images/items/bananapeeled.png");
	mRecipeModel << map;
	map["title"] = QString("Selection");
	map["image"] = QString("assets/images/items/olives.png");
	mRecipeModel << map;
	map["title"] = QString("Input");
	map["image"] = QString("assets/images/items/lemon.png");
	mRecipeModel << map;
	map["title"] = QString("Label");
	map["image"] = QString("assets/images/items/pear.png");
	mRecipeModel << map;
	map["title"] = QString("Picker");
	map["image"] = QString("assets/images/items/banana.png");
	mRecipeModel << map;
	map["title"] = QString("DropDown");
	map["image"] = QString("assets/images/items/beer.png");
	mRecipeModel << map;
	map["title"] = QString("ActivityIndicator");
	map["image"] = QString("assets/images/items/orange_sliced.png");
	mRecipeModel << map;
	map["title"] = QString("ProgressIndicator");
	map["image"] = QString("assets/images/items/peas_in_pod.png");
	mRecipeModel << map;
	map["title"] = QString("Color");
	map["image"] = QString("assets/images/items/strawberries.png");
	mRecipeModel << map;

    if (UiValues::instance()->device() != UiValues::DEVICETYPE_720X720) {
        // The orientation recipe is not added for devices that does not support it.
        map["title"] = QString("Orientation");
        map["image"] = QString("asset:///images/items/cucumber.png");
        mRecipeModel << map;
    }

	map["title"] = QString("DockLayout");
	map["image"] = QString("assets/images/items/docklayout.png");
	mRecipeModel << map;
	map["title"] = QString("Animation");
	map["image"] = QString("assets/images/items/egg.png");
	mRecipeModel << map;
	map["title"] = QString("Menu");
	map["image"] = QString("assets/images/items/menu.png");
	mRecipeModel << map;
	map["title"] = QString("Stock Curve");
	map["image"] = QString("assets/images/items/leek.png");
	mRecipeModel << map;
	map["title"] = QString("WebView");
	map["image"] = QString("assets/images/items/orange.png");
	mRecipeModel << map;
	map["title"] = QString("Sheet");
	map["image"] = QString("assets/images/items/eggplant.png");
	mRecipeModel << map;
	map["title"] = QString("Dialog");
	map["image"] = QString("assets/images/items/sugarcandy.png");
	mRecipeModel << map;
	map["title"] = QString("GestureHandler");
	map["image"] = QString("assets/images/items/cookie.png");
	mRecipeModel << map;
	map["title"] = QString("ImagePaint");
	map["image"] = QString("assets/images/items/sugar_tile.png");
	mRecipeModel << map;
	map["title"] = QString("PixelBuffer");
	map["image"] = QString("assets/images/items/pixelbuffer.png");
	mRecipeModel << map;
	map["title"] = QString("RichText");
	map["image"] = QString("assets/images/items/alphabet_soup.png");
	mRecipeModel << map;
	map["title"] = QString("SegmentedControl");
	map["image"] = QString("assets/images/items/segmented_soup.png");
	mRecipeModel << map;
    map["title"] = QString("Invocation");
    map["image"] = QString("assets/images/items/chocolate.png");
    mRecipeModel << map;
	map["title"] = QString("Shortcuts");
	map["image"] = QString("assets/images/items/shortcut.png");
	mRecipeModel << map;
	map["title"] = QString("CustomPicker");
	map["image"] = QString("assets/images/items/custompicker.png");
	mRecipeModel << map;
	map["title"] = QString("Accessibility");
	map["image"] = QString("assets/images/items/sesame.png");
	mRecipeModel << map;
    map["title"] = QString("TextStyle");
    map["image"] = QString("assets/images/items/flour.png");
    mRecipeModel << map;
	recipeListView->setDataModel(&mRecipeModel);
	recipeListView->setListItemProvider(recipeItemManager);

	//recipeListView->setVerticalAlignment(VerticalAlignment::Fill);

	// Connect to the selection changed signal, used to update the recipe in the content area.
	connect(recipeListView, SIGNAL(triggered(const QVariantList)), this,
			SLOT(onTriggered(const QVariantList)));

	return recipeListView;
}

 

 

but i need to loop over the json file, insert the name and last name.

 

what does this sign mean <<  mrecipemodel << map

Developer
Posts: 540
Registered: ‎08-31-2013
My Device: Q10, Z10LE, Nokia N950, DAB....

Re: Loop over json file

<< Appends value



If helped give a like. If your problem was solved mark the post as "Solution"
Portfolio: BlackBerry Nokia
Social: Twitter | Facebook
About Me / Blog DevPDA | Blog