【发布时间】:2013-07-22 00:55:26
【问题描述】:
我有两个 appengine 应用程序,并且正在从一个应用程序中提供 JSONObject 的字符串表示形式,并在另一个应用程序中提取它。如果我不在 JSON 中包含 Text 对象,一切都会正常运行
这是导致问题的 JSON 对象的特定部分:
,\"text\":\u003cText: rthBlog 1\r\n\"如果你不\u0027t从历史中吸取教训,你注定要重复我...\u003e,
这是字符串形式的样子:
以下是将字符串放入数据存储区的相关代码[我正在使用 json.simple]:
文本 item_text = new Text("默认文本"); //它被超过500个字符的文本填充 JSONObject j = new JSONObject(); j.put("文本", item_text); j.put("item_links", j_links); item.setProperty("as_json", j.toJSONString()); datastore.put(item);
这里是检索它的代码,将它包装在一个 JSONArray 中,一个 JSONobject 中的数组并生成一个字符串 [我正在使用 appengine json]:
JSONArray search_results = new JSONArray();
for(Entity e: items)
{
String j = (String) e.getProperty("as_json");
JSONObject jo;
if(j != null)
{
System.out.println(TAG + ".searchItems() string as json: " + j);
jo = new JSONObject();
jo.put("item", j);
search_results.add(jo);
}
}
JSONObject jo = new JSONObject();
jo.put("items", search_results);
return jo.toJSONString();
这是提取它的代码 [我正在使用 appengine json]:
试试 { JSONObject jsonObject = 新的 JSONObject(s); JSONArray jsonArray = (JSONArray) jsonObject.get("items");
JSONObject array_member = null;
JSONObject j;
for(int i=0; i<jsonArray.length(); i++)
{
array_member = jsonArray.getJSONObject(i);
System.out.println("array member" + array_member);
/*Text text = (Text)array_member.get("text"); //
System.out.println(text.getValue());*/
String s_item = array_member.getString("item");
System.out.println("item in string form: " + s_item);
j = new JSONObject(s_item); //This is the exception causing line
【问题讨论】:
-
我想在 GAE/J 中处理 JSON,这可能会有所帮助:bit.ly/13eSDpr
-
谢谢 xybrek 我可以在没有 maven 的情况下导入它吗?
-
你可以在这里下载:bit.ly/15HBbu3
-
xybrek 我下载并安装了。如何在另一个类中检索相同的数据库?Mungo mungo = new Mungo(); DB testDB = mungo.getDB("testDB"); DBCollection 问候 = null; //?????? DBObject greeting = greetings.findOne("{'username' : 'jack'}"); // 得到它
-
哦,你可以这样做,DBCollection greetings = testDB.createCollection("Message");
标签: java json google-app-engine servlets google-cloud-datastore