Java如何获取JSON的内容
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java如何获取JSON的内容相关的知识,希望对你有一定的参考价值。
第一张图是需要获取的内容,我想要获取rsp,第二章图是我写的代码,我想的是String获取rsp,然后转成JSONArray,再迭代取出JSONObject,请问我这么写正确吗?
报错了A JSONArray text must start with '['
Java样例程序如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
public class DoJSON
public static void main(String[] args)
JSONArray employees = new JSONArray(); //JSON数组
JSONObject employee = new JSONObject(); //JSON对象
employee.put("firstName", "Bill"); //按“键-值”对形式存储数据到JSON对象中
employee.put("lastName", "Gates");
employees.add(employee); //将JSON对象加入到JSON数组中
employee.put("firstName", "George");
employee.put("lastName", "Bush");
employees.add(employee);
employee.put("firstName", "Thomas");
employee.put("lastName", "Carter");
employees.add(employee);
System.out.println(employees.toString());
for(int i=0; i<employees.size(); i++)
JSONObject emp = employees.getJSONObject(i);
System.out.println(emp.toString());
System.out.println("FirstName :\t" + emp.get("firstName"));
System.out.println("LastName : \t" + emp.get("lastName"));
运行效果:
["firstName":"Bill","lastName":"Gates","firstName":"George","lastName":"Bush","firstName":"Thomas","lastName":"Carter"]
"firstName":"Bill","lastName":"Gates"
FirstName : Bill
LastName : Gates
"firstName":"George","lastName":"Bush"
FirstName : George
LastName : Bush
"firstName":"Thomas","lastName":"Carter"
FirstName : Thomas
LastName : Carter 参考技术B 这样的用法不对,rsp是JSONArray格式数据,不能用getString取出,只能用JSONArray rsp = xml.getJSONArray("rsp")取出然后遍历 参考技术C
求采纳
public static void main(String[] args)String json = "\\"xml\\":\\"code\\":\\"0\\",\\"nonceStr\\":\\"EAGEGEGAWGWGWAGWAGWAG\\",\\"rsp\\":[\\"aac02\\":\\"59682656655\\",\\"aac02\\":\\"59682656656\\",\\"aac02\\":\\"59682656657\\"]";
JSONObject jsonObj = JSONObject.fromObject(json);
JSONObject xml = jsonObj.getJSONObject("xml");
System.out.println(xml);
JSONArray rsp = xml.getJSONArray("rsp");
List<DataRsp> list = new ArrayList<>();
for (int i = 0; i < rsp.size(); i++)
DataRsp data = (DataRsp) JSONObject.toBean(rsp.getJSONObject(i),DataRsp.class);
list.add(data);
System.out.println(list);
public class DataRsp
private String aac02;
public String getAac02()
return aac02;
public void setAac02(String aac02)
this.aac02 = aac02;
@Override
public String toString()
return "DataRsp [aac02=" + aac02 + "]";
Java如何读取网址中的json内容
http://www.cashl.edu.cn/cashlsearch/search?json="targetTypes":"typeCode":"book","conditions":["logic":"AND","searchType":"INC","term":"b","column":"string_titlefacet"],"pageUtil":"currentPage":1,"pageSize":"20","maxResult":100
参考技术A String json = request.getParameter("json");// 以下为获取typeCode的代码
JSONObject jsonObject = JSONObject.fromObject(json);
JSONObject targetObject = jsonObject.getJSONObject("targetTypes");
String type = targetObject.getString("typeCode");
System.out.println(type);
获取其他属性,以此类推就可以了追问
您好,能不能给具体的类似的代码,我刚开始学习这个,谢谢
追答啊?具体的类似代码是指什么?
追问我想获取 dataMap":"typeid":"value":"9678023","title":"value":"Assessment for reading instruction / Michael C. McKenna, Katherine A. Dougherty Stahl.",
里面typeid的内容是 9678023 怎么修改?
谢谢
以上是关于Java如何获取JSON的内容的主要内容,如果未能解决你的问题,请参考以下文章