我正在从API中读取一组JSON对象,并希望使用Mustache以漂亮的HTML格式显示数据。
我从pre-built .jar file下载了https://github.com/samskivert/jmustache,并毫无问题地运行了hello world:
String text = "One, two, {{three}}. Three sir!";
Template tmpl = Mustache.compiler().compile(text);
Map<String, String> data = new HashMap<String, String>();
data.put("three", "five");
Log.d("json",tmpl.execute(data));
如何将JSONObject提供给Mustache?
我试过了:
JSONObject eventJSON = events.getJSONObject(event_count);
String text = "<h1>{{summary}}</h1>";
Template tmpl = Mustache.compiler().compile(text);
Log.d("this breaks", tmpl.execute(eventJSON));
Logcat说"No key, method or field with name 'summary' on line 1"
发送JSON字符串时出现同样的错误:
Log.d("this breaks", tmpl.execute(eventJSON.toString()));
eventJSON.toString()会导致really long string启动
{"summary":"the LUSTER a.k.a. VJ WADAKENによるウィークデーモンスターパーティー。\r\n","invitations":[],"genres":[1,2],"open_date":"2011-11-17"...
我必须convert the JSON to a hash吗?
Logcat完整错误:
11-17 12:04:19.080: E/AndroidRuntime(17227): FATAL EXCEPTION: AsyncTask #2
11-17 12:04:19.080: E/AndroidRuntime(17227): java.lang.RuntimeException: An error occured while executing doInBackground()
11-17 12:04:19.080: E/AndroidRuntime(17227): at android.os.AsyncTask$3.done(AsyncTask.java:266)
11-17 12:04:19.080: E/AndroidRuntime(17227): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
11-17 12:04:19.080: E/AndroidRuntime(17227): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
11-17 12:04:19.080: E/AndroidRuntime(17227): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
11-17 12:04:19.080: E/AndroidRuntime(17227): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
11-17 12:04:19.080: E/AndroidRuntime(17227): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1081)
11-17 12:04:19.080: E/AndroidRuntime(17227): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:574)
11-17 12:04:19.080: E/AndroidRuntime(17227): at java.lang.Thread.run(Thread.java:1020)
11-17 12:04:19.080: E/AndroidRuntime(17227): Caused by: com.samskivert.mustache.MustacheException: No key, method or field with name 'summary' on line 1
11-17 12:04:19.080: E/AndroidRuntime(17227): at com.samskivert.mustache.Mustache$VariableSegment.execute(Mustache.java:520)
11-17 12:04:19.080: E/AndroidRuntime(17227): at com.samskivert.mustache.Template.executeSegs(Template.java:70)
11-17 12:04:19.080: E/AndroidRuntime(17227): at com.samskivert.mustache.Template.execute(Template.java:58)
11-17 12:04:19.080: E/AndroidRuntime(17227): at com.samskivert.mustache.Template.execute(Template.java:48)
11-17 12:04:19.080: E/AndroidRuntime(17227): at com.example.android.events.AsyncJSONEventParser.parseEventJSON(AsyncJSONEventParser.java:88)
11-17 12:04:19.080: E/AndroidRuntime(17227): at com.example.android.events.AsyncJSONEventParser.doInBackground(AsyncJSONEventParser.java:40)
11-17 12:04:19.080: E/AndroidRuntime(17227): at com.example.android.events.AsyncJSONEventParser.doInBackground(AsyncJSONEventParser.java:1)
11-17 12:04:19.080: E/AndroidRuntime(17227): at android.os.AsyncTask$2.call(AsyncTask.java:252)
11-17 12:04:19.080: E/AndroidRuntime(17227): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
答案 0 :(得分:0)
json不能用作字符串,而且确实需要哈希......我和杰克逊一样。见post about jackson parsing in 5 minutes
@SuppressWarnings("unchecked")
protected void parseJsonString(String json){
try {
ObjectMapper mapper = new ObjectMapper();
//Object root = mapper.readValue(json, Object.class);
//Map<String,String> rootAsMap = mapper.readValue(json, Map.class);
//rails 3.0.3 is bad and doesn't qoute strings
userData = (Map<String,Object>) mapper.readValue(json, Map.class);
} catch (JsonParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JsonMappingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}