How to parse the JSON response in Blackberry/J2ME?

自闭症网瘾萝莉.ら 提交于 2019-11-26 14:26:52

问题


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??


回答1:


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




回答2:


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/




回答3:


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




回答4:


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.




回答5:


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




回答6:


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


来源:https://stackoverflow.com/questions/1470406/how-to-parse-the-json-response-in-blackberry-j2me

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!