Need suggestion for to write QString to JSON format - json

I need suggestion for my requirement of writting the QString data to json format.
Im reading data from dbus which returns me data in QString and QVariantMap and im using the data for my Qt GUI.
at the same time i have to feed the data to a web application .
the web application developer asked me to give the data in JSON format so he can read and write the data in his application .
so can you people suggest me a good way of writtin the data which comes from DBUS every time from DBUS to the JSON format .
Please provide me if any alternate solution or how i can synchronously write a JSON file .

A JSON object is quite similar to a map. If you're using Qt 5 writing data to JSON objects is very easy:
// String you would like to send to the web application
QString myString("Hi there!");
// JSON object used to store the data to be sent to the web application
QJsonObject myObject;
myObject.insert("key used by the web application", myString);
QJsonDocument myDocument(myObject);
At this point you can use myDocument to obtain the JSON representation of your data, by means of toJson() method. As an example:
qDebug() << myDocument.toJson();
produces this output:
{
"key used by the web application": "Hi there!"
}

Have a look at this link. It adds the support for JSON format in QT if you are using Qt 5.
I have not used it yet, but might be helpful.
The example of using the JSON with Qt can be found in this example.

Related

How do I convert the following data serialization format into POJO or JSON?

I was expecting a JSON string while testing an API using Postman, but instead got this:
{city=Shanghai, work=112-454-7895, fax=788-899-7899}
Obviously I cannot put that into Google and ask what format is it, hence I am asking it here. Postman also says it is a 'bad string'.
I have never seen the above data serialization format. If someone can point the format out to me I would be able to find and use a converter. Additional suggestion with converting it to POJO or JSON are welcome as well.
I figured out that the above format is a stringified version of a HashMap<String, String>, and that there is no direct way to convert it into JSON/POJO. Converting it into json string requires additional work but is fairly straightforward.

Grails - Error saving JSONObject to MongoDB

I am having trouble saving a JSONObject to a MongoDB database using the MongoDB plugin.
I receive the message:
Can't find a codec for class org.codehaus.groovy.grails.web.json.JSONObject..
This is very frustrating because I am using the JSON parser to load JSON data but can't persist this JSON data to the MongoDb which should be straightforward.
Is there a built in way to convert a JSONOBject to a normal Map? I've tried casting it using asType( Map ), ( Map ), and even using toString() and thent rying to convert back from string to object. I've seen that other vanilla Java questions involve using Jackson but I'm hoping there is a Groovier way to do this rather than importing a whole new library for just two lines of code.
This is what I'm doing for now:
Converting the JSONObject to a string and then using com.mongodb.util.JSON.parse() to convert that string to a DBObject that Mongo can use.
It's not the best but it works for now.
I'm not going to accept this answer because I don't think it's the right answer.
Not saying this is the correct answer, but I was able to convert the JSONObject to a HashMap. For my situation I had a Domain object with an ArrayList (converted from JSONArray by a previous JSONTranslationService) and I was able to convert each of the internal JSONObjects using something like this:
static final UNMARSHAL = { thing ->
thing.objects.collect {
it as Hashmap
}
}
I'm only experiencing this issue after an upgrade from mongodb:3.0.2 to 6.1.2 to support MongoDB 3.4. Are you also running this version of the plugin? If so, I think it's fair to say that there's either a bug in the plugin (I'm already aware of one) or something changed with the default behavior and wasn't documented.

Create a new JSON file in QT5

Obviously Qt5 has better support for JSON. The Qt example http://doc.qt.io/qt-5/qtcore-json-savegame-example.html explains how to parse and also modify JSON files or object, which is great.
But for my requirement I should create a completely new JSON file, so I can't use the methods to modify existing JSON parameters. Maybe I didn't understand the examples, but how can I create a completely new JSON object?
thanks!
Should be rather straight forward:
QJsonObject myJsonObj;
myJsonObj["MyValue"] = 10;
QJsonDocument doc(myJsonObj);
QFile file("MyFile.json");
file.open(QIODevice::WriteOnly | QIODevice::Text);
file.write(doc.toJson(QJsonDocument::Indented));

Anyone figure out how to save a JSON string into a DocumentDB collection?

Am trying to make a quick and dirty NLog DocumentDB target but can't seem to be able to directly save JSON into a DocumentDB.
Using the C# library seems the document parameter of DocumentClient.CreateDocumentAsync() only wants a 'complex type' and I see no other methods that might take a JSON string.
Anybody else figured out how to save JSON directly?
Here's a sample of how you can save a JSON string using DocumentDB using the C# SDK.
using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(#"{ a : 5, b: 6, c: 7 }")))
{
await client.CreateDocumentAsync(collectionSelfLink,
Document.LoadFrom<Document>(ms));
}

How to parse the JSON response in Blackberry/J2ME?

I want to parse the response coming from the server in JSON format. I have done some googling but i can't find any library or jar kind of thing.
Everywhere there is provided open source code as zip file.
How can i achieve this? if there is no jar available for blackberry then how to use that open source code in my application??
There is an open source JSON for J2ME API on Mobile and Embedded Application Developers Project
Also you can download JSON ME (Zip file) at JSON.org not supported anymore. But you can get it from here.
I believe you can simply copy content of json project src folder to your Blackberry project src folder, refresh it in eclipse package explorer and build it.
See for details: Using JavaScript Object Notation (JSON) in Java ME for Data Interchange
I am developing a Blackberry client application and I confronted the same problem. I was searching for JSON way of parsing the response which I get from the server. I am using Eclipse plug-in for BB as IDE and it comes with BB SDK including the ones for JSON.
The easiest answer to this question is that:
Initially do not forget to use this import statement:
import org.json.me.JSONObject;
Then assign your JSON formatted response from the server to a String variable:
String jsonStr = "{\"team\":\"Bursaspor\",\"manager\":\"Ertuğrul Sağlam\",\"year\":\"2010\"}";
Create a JSONObject:
JSONObject obj = new JSONObject(jsonStr);
i.e. if you want to use the value of the "team" field which is "Bursaspor" then you should use your JSONObject like this:
obj.getString("team")
This call will return the string value which is "Bursaspor".
P.S: I inspired this solution from this site which simply explains the solution of the same problem for Android development.
http://trandroid.com/2010/05/17/android-ile-json-parse-etme-ornegi-1/
When you got response string then use this code
try {
JSONObject jsonres = new JSONObject(jsons);
System.out.println("Preview icon from jsonresp:"+jsonres.getString("mydata"));
} catch (JSONException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
one thing jsons is your response string which is in json format & mydata your key of data so you can get your data from JSONObject. Thnks
chek this post to find out how to get JSONME source from SVN:
http://www.java-n-me.com/2010/11/how-to-import-source-code-from-svn-to.html
Hope it helps someone.
You can also try :
http://pensivemode.fileave.com/verified_json.jar
I was also looking for a jar version of json (to link along my lib) :
https://radheshg.wordpress.com/tag/json/
but it seems not portable :
Building example
C:\Program Files (x86)\Research In Motion\BlackBerry JDE 4.5.0\bin\rapc.exe -quiet import="C:\Program Files (x86)\Research In Motion\BlackBerry JDE 4.5.0\lib\net_rim_api.jar";lib\${PROJECT}.jar;lib\json.jar codename=example\example example\example.rapc warnkey=0x52424200;0x52525400;0x52435200 Y:\src\${PROJECT}-java.git\example\src\mypackage\MyApp.java Y:\src\${PROJECT}-java.git\example\src\mypackage\MyScreen.java
tmp3139/org/json/me/JSONArray.class: Error!: Invalid class file: Incorrect classfile version
Error while building project
http://supportforums.blackberry.com/t5/Java-Development/JSON-library/m-p/573687#M117982
So far JSON.simple is looking like a great viable option.
JSONParser parser=new JSONParser();
System.out.println("=======decode=======");
String s="[0,{\"1\":{\"2\":{\"3\":{\"4\":[5,{\"6\":7}]}}}}]";
Object obj=parser.parse(s);
JSONArray array=(JSONArray)obj;
System.out.println("======the 2nd element of array======");
System.out.println(array.get(1));
System.out.println();