MasterofProject

2 CW Liyuan Liyuan_cw15:54 2013.05.30 questions

Java how to read JSON format data

Java how to read JSON format and then displayed in front of web game development, urgently ah, thank you!

100 answers

Guwei4037
Guwei4037   Ds   Rxr 12:07 2014.11.23
Has adopted
/ * *
*
* result JSON @param string
* name JSON @param array name
* fields JSON @param string contained in the field
* @return return List<Map<String list, Object>> type Map<String, Object> corresponds to "Id": "1" structure
* /
Static List<Map<String Object>>, convertJSON2List public (result String,
Name String, fields String[]) {
Object>>, list List<Map<String = ArrayList<Map<String Object>>, new ();
{try
Array JSONArray = JSONObject new (result).GetJSONArray (name);

For (I int = 0; I < array.length (); i++) {
Object = (JSONObject) array.opt (I) JSONObject ();
Object>, map Map<String = HashMap<String Object>, new ();
For (STR String: fields) {
Map.put (STR, object.get (STR));
}
List.add (map);
}
{catch} (E JSONException) {
Log.e ("error", e.getMessage ());
}
List return;
}
Guwei4037
Guwei4037 Reply Log Android is inside the baidu_30317447:, you can either write a log class.
5 months ago Reply
Baidu_30317447
Baidu_30317447 How to download the LOG package
5 months ago Reply
Shuipingzildy
Shuipingzildy   18:04 2013.06.03

Did some JSON data analysis of several commonly used JSON data format analysis

Static Person getPerson public String (key String, jsonString) {
Person Person = Person new ();
{try
JsonObject JSONObject = JSONObject new (jsonString);
PersonObject JSONObject = jsonObject.getJSONObject ("person");
Person.setId ("personObject.getInt") ("Id");
Person.setName ("personObject.getString") ("name");
Person.setAddress ("personObject.getString") ("address");
{catch} (E Exception) {
Handle exception / / TODO:
}
Person return;
}

Static List<Person> getPersons public String (key String, jsonString) {
List List<Person> = ArrayList<Person> new ();
{try
JsonObject JSONObject = JSONObject new (jsonString);
/ / return an array of JSON
JsonArray JSONArray = jsonObject.getJSONArray (key);
For (I int = 0; I < jsonArray.length (); i++) {
JsonObject2 JSONObject = jsonArray.getJSONObject (I);
Person Person = Person new ();
Person.setId ("jsonObject2.getInt") ("Id");
Person.setName ("jsonObject2.getString") ("name");
Person.setAddress ("jsonObject2.getString") ("address");
List.add (person);
}
{catch} (E Exception) {
Handle exception / / TODO:
}
List return;
}

Static List<String> getList public String (key String, jsonString) {
List List<String> = ArrayList<String> new ();
{try
JsonObject JSONObject = JSONObject new (jsonString);
JsonArray JSONArray = jsonObject.getJSONArray (key);
For (I int = 0; I < jsonArray.length (); i++) {
MSG String = jsonArray.getString (I);
List.add (MSG);
}
{catch} (E Exception) {
Handle exception / / TODO:
}
List return;
}

Static List<Map<String public, getListMap Object>> (key String, jsonString String) {
Object>>, list List<Map<String = ArrayList<Map<String Object>>, new ();
{try
JsonObject JSONObject = JSONObject new (jsonString);
JsonArray JSONArray = jsonObject.getJSONArray (key);
为(int i = 0;i < jsonarray。length();i++){
JSONObject jsonobject2 = jsonarray getjsonobject(我);
图<字符串对象>地图=新的HashMap <字符串对象>();
迭代器<字符串> = jsonobject2 keys()迭代器;
而(迭代器。hasnext()){
字符串json_key = next()迭代器;
对象json_value = jsonobject2得到(json_key);
如果(json_value = = null){
json_value =“”;
}
地图。把(json_key,json_value);
}
添加(图);
}
(例外情况){
// TODO:处理异常
}
返回列表;
}
Xiashuangxi
xiashuangxi   2014.11.19 14:12

这个第三方的JSON库有很多的,杰克逊gson,JSON库,flexjson,JSON的IO,根森,jsonij等,都很好使用的,个人比较喜欢JSON库,gson

U010244790
u010244790   2014.12.29 14时06分

下面是关于JSON的博客文章,你可以参考一下
http://hi.baidu.com/yangkailin0123/item/b49a9fd46709e5856cce3f53
http://wangcheng2008china.blog.163.com/blog/static/128635503201152435321527/

Java中处理JSON数据使用到的包:
commons-beanutils.jar
commons-collections-3.2.jar
commons-httpclient-3.0.jar
commons-lang-2.5.jar
commons-logging-1.0.jar
ezmorph-1.0.4.jar
json-lib-2.4-jdk15.jar

QQ372252612
qq372252612   2013.05.30 16:26

框架那就好办了如果有用Struts,Struts,XML里配置继承JSON defual,一般Struts包都有,在行动下个方法,一样的返回结果XML中Struts。

<结果名称=“返回字符串”type=“json”>
<参数名=“includeproperties”>传出去的值,传出去的值,…< /参数>
< /结果>

页面异步刷新的写法

CSDNPlus
csdnplus 回复csdnplus:
大约一年之前 回复
CSDNPlus
csdnplus 回复qq_24636087:
大约一年之前 回复
Qq_24636087
qq_24636087 不知道
大约一年之前 回复
Suannai0314
suannai0314 帮你编辑了下代码的格式,更多降价使用见http://prog3.com/sbdm/ask/pages/markdown
2年多之前 回复
A576699534
a576699534   2015.02.03 23:45

用JS来读,然后传给JSP。

Kobi521
kobi521   2013.05.30 21:34

用org。开头的JSON的罐包里面有objectjson你可以试试

Guo15889260921
guo15889260921   2013.05.31 15:29

网上有很多专业把JSON格式数据转换成对象和把对象转成JSON格式数据的你可以搜下

U013072041
u013072041   2014.11.18 11:08

单个的JSON数据用:JSONObject JSONObject = JSONObject从对象(json_str)。
数组用:jsonarray jsonarray = jsonarray从对象(json_str);

100条数据共 尾页
User default icon Csdn
上传中…
上传图片
插入图片