is it possible to proccess JSON responses with the JDK or HttpComponents only?

最后都变了- 提交于 2019-11-26 14:49:27

问题


we are upgrading our web app to use Facebook's Graph API, which returns JSON responses. However we don't want to add dependecy to a JSON library unless we have no other choice. For server-side http requests we use Apache HttpComponents.

Thus, my question is what are the classes (if any) in the JDK and/or in HttpComponents that I can use to process JSON responses? Code snippets are welcome :)


回答1:


It is possible. Because JSON is valid JavaScript syntax, you can use the built-in JavaScript interpreter via the scripting API to create and object graph, walk that (using the visitor pattern to push data into a Java object, for example).

However, you need to trust the data or you leave yourself open to code injection attacks. To me, this would not be an adequate substitute for a proper JSON parser.




回答2:


There are none is the javax.script API as mentioned by @McDowell, but the JSON syntax is pretty trivial. You could write a stackbased parser yourself which determines the JSON string char-by-char and converts JSON arrays [] to a Object[] or List<Object> and JSON objects {} to a Map<String, Object>. It's a nice learning exercise. However, when you don't want to worry about long term robustness and maintenance, then I'd suggest to just pick one of the Java libraries listed at the bottom of this page. My personal favourite is Google Gson. It can nicely convert complex JSON structures into fullworthy and reuseable Javabeans.




回答3:


Unfortunately, native JSON support was delayed past Java 9.

But for the sake of sportmanship here is plain Java 8 hacky solution using Nashorn JavaScript engine without any external dependency:

String json = "{\"foo\":1, \"bar\":\"baz\"}";
ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn");
Object o = engine.eval(String.format("JSON.parse('%s')", json));
Map<String, String> map = (Map<String, String>) o;
System.out.println(Arrays.toString(map.entrySet().toArray()));
// [foo=1, bar=baz]

Since Java 8u60 JSON.parse can be substituted with Java.asJSONCompatible which does better handling of JSON arrays.

Credits:

Effective way to pass JSON between java and javascript

https://dzone.com/articles/mapping-complex-json-structures-with-jdk8-nashorn




回答4:


I think what you are looking for is the org.json package. You can get the source here and simply include the handful of files in your project, it doesn't have any dependencies. This will allows you do create and parse JSON. The javadocs are well done and can be found here.

As an example, for consuming json, you can use a tokener and convert the raw string to a JSONObject. Then you can access the arrays by index or by key. You can access nested arrays by getting them as a JSONObject or JSONArray.

JSONTokener tokener = new JSONTokener(myJsonString);
JSONObject json = new JSONObject(tokener);

String error = json.get("error");
int errorCode = json.getInt("error_code");

JSONArray messages = json.getJsonArray("messages");

Update: The source is also available at GitHub



来源:https://stackoverflow.com/questions/3970195/is-it-possible-to-proccess-json-responses-with-the-jdk-or-httpcomponents-only

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