How to parse the JSON response in Blackberry/J2ME? - json

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();

Related

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.

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));
}

Need suggestion for to write QString to JSON format

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.

How to convert String values to json object in java without using org.json

I'm trying to convert a JSON String value `{"name:ganesh,sex:male,age:22"} ) into key and value set using json in gwt can anyone help me with some ideas please.
Since you don't want to use org.json, I imagine that you need to convert JSON on the client side. If this is the case, you will need to use GWT's JSON libraries. They could be inherited into your GWT project by adding these lines to your .gwt.xml file:
<inherits name="com.google.gwt.json.JSON" />
<inherits name="com.google.gwt.http.HTTP" />
You'll also need to import the com.google.gwt.json packages into your class files.
The getting started guide to using JSON in GWT can be found here. It also provides examples for decoding JSON.
Your string must be of the form
"{'name':'ganesh','sex':'male','age':'22'}"
or
'{"name":"ganesh","sex":"male","age":"22"}'
You can use one of the following ways ...
Use javascript eval. Embed the eval in JSNI
public static native String eatString(String jstring) /*-{
eval("var hello = " + jstring + ";");
return hello;
}-*/;
Pass the String as script in a JSP GWT hosting file. This way, you can only doing once - when the GWT app is loaded with its hosting file. Your JSP will generate the javascript dynamically for each loading.
Place the following in hour GWT hosting html file, before the script tag where GWT module is called.
<script>
var hello = {'name':'ganesh','sex':'male','age':'22'};
</script>
Then in your GWT app, use the GWT Dictionary class to reference any javascript objects declared in the hosting file.
Use the following utilities, in particular JsonRemoteScriptCall.java to read remote, out of SLD-SOP javascript objects into your GWT app. http://code.google.com/p/synthfuljava/source/browse/trunk/gwt/jsElements/org/synthful/gwt/javascript/#javascript%2Fclient.
Please be warned - out of SLD-SOP objects can be hazardous. Read up on Second level domain, same origin policy browser security.
Use RestyGWT and pretend that the data from the server comforms to REST data structure. But of course, use of json.org and google JSON utils has already been done for you.

Firefox trying to download json from grails

I have a grails controller whose last line is
render results as JSON
It is being rendered to an iframe. When firefox processes this, it tries to download the json file. Is this normal? The only way I've been able to get around this is to convert it to a string and then handle it on the javascript side.
I have a grails controller in which I wrote something like this and I've got String message. Try this example.
String msg = g.message(code: "default.errors.login.fail")
render([error: msg] as JSON)