实现代码:
import net.sf.json.JSONArray; import net.sf.json.JSONObject; public class JsonTest { public static void main(String[] args) { String joStr = "{name:\"张三\",ahttp://www.cppcns.comge:\"20\"}"; //将json字符串转化为JSONObject JS编程客栈ONObject jsonObject = JSOhttp://www.cppcns.comNObject.fromObject(joStr); //通过getString("")分别取出里面的信息 String name = jsonObject.getString("name"); String age = jsonObject.getString("age"); //输出 张三 20 System.out.println(name+" "+age); String jaStr = "[{user:{name:\"张三\",age:\"20\"}},{score:{yuwen:\"80\",shuxue:\"90\"}}]"; //将jsonArray字符串转化为JSONArray JSONArray jsonArray = JSONArray.fromObject(jaStr); //取出数组第一个元素 JSONObject jUser = jsonArray.getJSONObject(0).getJSONObject("user"); //取出第一个元素的信息,并且转化为JSONObject String name2 = jUser.getString("name"); String age2 = jUser.getString("age"); www.cppcns.com //输出 张三 20 System.out.println(name2+" "+age2); //取出数组第二个元素,并且转化为JSONObject JSONObject jScore = jsonArray.getJSONObject(1).getJSONObject("score"); //取出第二个元素的信息 String yuwen = jScore.get编程客栈String("yuwen"); String shuxue = jScore.getString("shuxue"); //输出 80 90 System.out.println(yuwen+" "+shuxue); } }
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
本文标题: java json字符串转JSONObject和JSONArray以及取值的实例
本文地址: http://www.cppcns.com/wangluo/javascript/191866.html
© 著作权归作者所有
举报
发表评论
0/200