Namespaces
Variants
Actions
Revision as of 05:48, 4 March 2013 by hamishwillee (Talk | contribs)

How to parse JSON string

Jump to: navigation, search

JSON is a popular data representation format. This article shows how to parse a JSON string using Windows Phone and Symbian C++, and provides links to libraries that can be used to parse JSON in Qt.

MultiMediaTile.png
SignpostIcon Code 52.png
WP Metro Icon Web.png
Article Metadata

Code Example
Article
Created: vdharankar (17 Jul 2010)
Last edited: hamishwillee (04 Mar 2013)

JSON Parsing in Windows Phone

The code snippet below shows how the json2csharp and json.net are used to parse JSON. The code given below parses the response received by Google's translator which is in JSON format.

private void ParseJson()
{
var jsonString =
"{\"responseData\": {\"translatedText\":\"אני ילד\"}, \"responseDetails\": null, \"responseStatus\": 200}";
RootObject jsonObject = JsonConvert.DeserializeObject<RootObject>(jsonString);
MessageBox.Show(jsonObject.responseData.translatedText);
}
//This class was generated with json2csharp
public class ResponseData
{
public string translatedText { get; set; }
}
//This class was generated with json2csharp
public class RootObject
{
public ResponseData responseData { get; set; }
public object responseDetails { get; set; }
public int responseStatus { get; set; }
}

Symbian C++ parsing library

The Symbian C++ parser is called s60-json-library. It is hosted on code.google.com here and can be downloaded from: Media:S60-json-library.zip.

The code snippet below shows how the API is used to parse JSON. The code given below parses the response received by Google's translator which is in JSON format.

_LIT(KTestFormatedJson, "{\"responseData\": {\"translatedText\":\"אני ילד\"}, \"responseDetails\": null, \"responseStatus\": 200}");
 
CJsonBuilder* jsonBuilder = CJsonBuilder::NewL();
// this will create json string representation in memory
jsonBuilder->BuildFromJsonStringL(iKTestFormatedJson);
 
CJsonObject* rootObject;
CJsonObject* textObj;
jsonBuilder->GetDocumentObject(rootObject);
 
if(rootObject)
{
TBuf<256> message;
rootObject->GetObjectL(_L("responseData"),textObj);
textObj->GetStringL(_L("translatedText"),message);
}
 
// we need manually release created object
delete rootObject;
 
// releases only jsonBuilder object, not objects which was created by him
delete jsonBuilder;

JSON Parsing in Qt

  • QJson - a library for parsing JSON in Qt
482 page views in the last 30 days.
Nokia Developer aims to help you create apps and publish them so you can connect with users around the world.

京ICP备05048969号  © Copyright Nokia 2013 All rights reserved