我正在尝试在黑莓中使用JSON对象,但我遇到了问题。 JSON对象的创建始终返回异常。我使用的是JDE 6.0和RIM提供的JSON库。请在下面找到代码。有什么问题吗?
我正在创建一个JSON对象,然后从这个对象中取出String并再次转换它,它给了我一个错误:在...之后缺少“:”...
try {
JSONObject message = new JSONObject();
message.put("MessageId", "1");
message.put("TitleEn", "Shopping");
message.put("TextEn", "Shopping");
message.put("Date", "");
message.put("Price", "");
JSONObject message1 = new JSONObject();
message1.put("MessageId", "2");
message1.put("TitleEn", "Beauty");
message1.put("TextEn", "Beauty");
message1.put("Date", "");
message1.put("Price", "");
JSONObject message2 = new JSONObject();
message2.put("MessageId", "3");
message2.put("TitleEn", "Fashion");
message2.put("TextEn", "Fashion");
message2.put("Date", "");
message2.put("Price", "");
Vector elements = new Vector();
elements.addElement(message);
elements.addElement(message1);
elements.addElement(message2);
JSONArray array = new JSONArray(elements);
JSONObject obj = new JSONObject();
obj.put("Message", array);
JSONObject obj1 = new JSONObject();
obj1.put("TagId", "2");
obj1.put("NextTagId", "3");
obj1.put("Sequence", "1$$");
obj1.put("Content", obj);
JSONObject finalJson = new JSONObject();
finalJson.append("ListViewText", obj1);
String jsonString = finalJson.toString();
JSONTokener token = new JSONTokener(jsonString);
try {
JSONObject obj6 = new JSONObject(token);
String aaa = "aaa";
} catch (Exception ex) { }
} catch (Exception ex) { }
使用的字符串如下:
{"ListViewText":[{"TagId":"2","NextTagId":"3","Sequence":"1$$","Content":{"Message":[{"MessageId":"1","TitleEn":"Shopping","TextEn":"Shopping","Date":"","Price":""},{"MessageId":"2","TitleEn":"Beauty","TextEn":"Beauty","Date":"","Price":""},{"MessageId":"3","TitleEn":"Fashion Trends","TextEn":"Fashion Trends","Date":"","Price":""}]}}]}