jsonobject操作json

liujianliang 2013-04-26 03:02:57
json是从webservice获取的
返回的格式:{"item":{"orderId":"219761"},"code":0}

String json = doPost("http://192.168.2.176:8903/API/api", user);
JSONObject jsonObj = new JSONObject(json);
System.out.println(jsonObj.getString("item"));

行不通,这个json格式是不是还需要包装一下,求解
...全文
45 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
XZkaishi1989 2013-04-26
  • 打赏
  • 举报
回复
可以行的通,是不是别的问题啊
public class JsonTest {
	public static void main(String[] args) {
		//{"item":{"orderId":"219761"},"code":0}
		Map<String,Object> map = new HashMap<String, Object>();
		TestTo item = new TestTo();
		item.setOrderId("219761");
		map.put("item", item);
		map.put("code", 0);
		JSONObject jsonObj = JSONObject.fromObject(map);
		String s = jsonObj.toString();
		JSONObject jsonObject = new JSONObject(s);
		System.out.println(jsonObject.getString("item"));
	}
}
public class TestTo {
	public String orderId;

	public String getOrderId() {
		return orderId;
	}

	public void setOrderId(String orderId) {
		this.orderId = orderId;
	}
}
	}
我测试了下没有问题啊可以获取
使用JSONObject, JSONArray的全套完整jar包,绝对好用。另外附上例子一枚 package com.jjzy.test; import java.util.Iterator; import net.sf.json.JSONArray; import net.sf.json.JSONObject; public class TestJson { public static void main(String[] args) { String gg = "{\"name\":[\"啦啦\",\"哈哈\"]}"; JSONObject jsonObject = JSONObject.fromObject(gg); JSONArray jsonArr = jsonObject.getJSONArray("name"); @SuppressWarnings("unchecked") Iterator it = jsonArr.iterator(); while(it.hasNext()){ String ff = (String)it.next(); System.out.println("获取的是:"+ff); } } }

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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