java怎么读取json数据呢

wangmeiling00 2011-08-26 08:23:17
java怎么读取json数据呢
...全文
1080 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
凌风雪雕 2011-08-27
  • 打赏
  • 举报
回复
这里就是json字符串,还算比较复杂的一个了吧 {"icache":[{"cache_server_address":"10.88.53.198:11201","app_name":null,"cache_server_id":null,"token":null},{"cache_server_address":"10.88.53.198:11200","app_name":null,"cache_server_id":null,"token":null}]}

先到http://download.csdn.net/source/3512951下载jar包,然后

private static List<String> getListFromJson(String json) {

JSONObject ObjList;
List<String> list = new ArrayList<String>();
try {
ObjList = new JSONObject(json);
if (ObjList.has("icache")) {
JSONArray jArray = ObjList.getJSONArray("icache");
for (int i = 0; i < jArray.length(); i++) {
JSONObject jObj = (JSONObject) jArray.get(i);
list.add(jObj.getString("cache_server_address"));
}
}
} catch (Exception e) {
logger.error(e);
list = null;
} finally {
logger.info("ServerList is " + list);
return list;
}
}
凌风雪雕 2011-08-27
  • 打赏
  • 举报
回复
http://download.csdn.net/source/3512951
liyang1271989 2011-08-27
  • 打赏
  • 举报
回复
好多都有json,
例如: struts2 xwork 等框架都包含,
单独有json-lib这个开源的关联的比较多,要是用jdk1.4会出错,官网发布的jdk1.4相应的包有bug,不知道现在改了没

还有一种org.json 这是轻量级的,可以使用!

现在好多json的开源包,lz可以找一种试试
huntor 2011-08-26
  • 打赏
  • 举报
回复
找个第三方库 如 jackson / gson 等。
luohuijun619 2011-08-26
  • 打赏
  • 举报
回复
看个js的处理

{   
userName: "nihao",
sex: "male",
age: "23"
}


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<!-- 导入两个js文件 -->
<script type="text/javascript" src="./ajax.js"></script>
<script type="text/javascript" src="./prototype1.6.js"></script>
<title>jsontest.html</title>

<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=GB18030">
<!--<link rel="stylesheet" type="text/css" href="./styles.css">-->

<script type="text/javascript">
var req = createXMLHttpRequest();
function startRequest(){
try{
req.onreadystatechange = handleStateChange;
req.open("GET", "user.txt", true);
req.send(null);
}catch(exception){
alert("");
}
}
function handleStateChange(){
if(req.readyState == 4){
if (req.status == 200 || req.status == 0){
// 取得返回字符串
var resp = req.responseText;
// 构造返回JSON对象的方法
var func = new Function("return " + resp);
// 得到JSON对象
var json = func( );
// 显示返回结果
alert("userName: " + json.userName + " " + "sex: " + json.sex + " " + "age: " + json.age);
}
}
}
</script>
</head>
<body>
<div>
<input type="button" value="json's value"
onclick="startRequest();" />
</div>
</body>
</html>


http://www.iteye.com/topic/231788
luohuijun619 2011-08-26
  • 打赏
  • 举报
回复
json最好用js读取,非常方便,就跟处理对象一样简单

package test;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class StrJson1 {

public static void main(String[] args) {
String json = "{'name': '呵呵','array':[{'a':'111','b':'222','c':'333'},{},{'a':'999'}],'address':'上海'}";
try {
JSONObject jsonObject = new JSONObject(json);
String name = jsonObject.getString("name");
String address = jsonObject.getString("address");

System.out.println("name is:" + name);

System.out.println("address is:" + address);

JSONArray jsonArray = jsonObject.getJSONArray("array");

for (int i = 0; i < jsonArray.length(); i++) {
System.out.println("item " + i + " :" + jsonArray.getString(i));
try{
JSONObject jsonObject333 = new JSONObject(jsonArray.getString(i));
String aaaa = jsonObject333.getString("a");
System.out.println("----------"+aaaa);
}catch(Exception e){
}
}
} catch (JSONException e) {
e.printStackTrace();
}


}
}
softroad 2011-08-26
  • 打赏
  • 举报
回复
有两种json.jar
用法不同,建议lz找点实例。
softroad 2011-08-26
  • 打赏
  • 举报
回复
找个json.jar里面有方法

51,409

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧