Parse JSON with org.json [duplicate]

大城市里の小女人 提交于 2019-11-28 09:46:00
JohnnyO

1) Assuming you have the JSON libraries on your path (from www.json.org), it's pretty easy.

import org.json.JSONTokener;
...

URI uri = new URI("http://someserver/data.json");
JSONTokener tokener = new JSONTokener(uri.toURL().openStream());
JSONObject root = new JSONObject(tokener);

From there, you can address the various parts of the JSON object. Take a look at the Javadocs for the specifics. https://developer.android.com/reference/org/json/package-summary.html

Nevkontakte

Here is the most universal solution, which allows to parse any JSON type into appropriate Java type:

Object json = new JSONTokener(response).nextValue();

Then you can determine resulting type and handle it appropriately.

TravMan

I'd pass it

map<String, Object> 

loaded with

map<String, Object> 

in the object field.

Basically recreating the hierarchy of your java classes inside of a large map.

Example :

return ( Map<"GetFolderFilesByZoneResult", Map<"Result1", (object by id 98748) | "Result2", (object by id 99107) | "Result3", etc.

JSON will return that big map very pretty like, and programmatically it's easier to do then lists.

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