Parsing the JSON formatted string/Array from the service in Blackberry
I am trying to send some data to a server, so once the data is received or any error occurs while the transaction is processing, the server would intimate to its clients through a JSON type of response. when i post a a set value , as i said, its it returns a JSON format like the one below,
{"code":0,"err":"Missing 'method'."}. // JSON format returned from t开发者_JAVA技巧he server
/** Method that is responsible for converting the JSON format to other types*/
public static void convertFromJSON(String json) throws JSONException {
// the string json holds JSON format string that i mentioned above
// Creates a JSONArray with the values provided in the string json
JSONArray entries = new JSONArray(json); // error : The constructor JSONArray(String) refers to the missing type JSONException
}
I need to parse this JSON format and evaluate it depending upon the value that it returns. I have imported the appropriate JSON JAR too.
Any help is appreciated
Thanks.
Try the following.
JSONObject row = new JSONObject(json);
String code = row.get("code").toString();
String err = row.get("err").toString();
I'd recommend http://code.google.com/p/json-simple/:
import org.json.simple.JSONObject;
import org.json.simple.JSONValue;
public static void convertFromJSON(String json){
JSONObject entries = JSONValue.parse(json);
}
Also, the sample you've provided won't parse into a JSONArray.
精彩评论