注:{}内为JSONObject对象,而[]内为JSONArray对象
数据:{"code":"0","data":{"block":[{"type":"text","line":[{"confidence":1,"word":[{"content":"#接口调用流程"}]}]}]},"desc":"success","sid":"wcr005108b0@gz50bf12296549463000"}
这是call 图像识别的api获取的结果。
利用Java代码解析
引入jar:
<!-- JSONObject对象依赖的jar包 -->
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>1.9.3</version>
</dependency>
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
<version>3.2.1</version>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>net.sf.ezmorph</groupId>
<artifactId>ezmorph</artifactId>
<version>1.0.6</version>
</dependency>
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.2.3</version>
<classifier>jdk15</classifier><!-- 指定jdk版本 -->
</dependency>
String result = HttpUtil.doPost1(WEBOCR_URL, header, "image=" + URLEncoder.encode(imageBase64, "UTF-8"));
JSONObject jsonObject= JSONObject.fromObject(result);
System.out.println(jsonObject);
//获取data的json
JSONObject dataJson = (JSONObject) jsonObject.get("data");
/**
* 获取data下block的jsonArray,取其第一个array
*/
JSONArray jsonArray = dataJson.getJSONArray("block");
Object blockObject = jsonArray.get(0);
/**
* 将第一个array转化为JSONObject
*/
JSONObject blockJson= JSONObject.fromObject(blockObject);
Object lineObject = blockJson.getJSONArray("line").get(0);
/**
* 将line下的第一个数组转化成JSONObject
*/
JSONObject worldJson= JSONObject.fromObject(lineObject);
/**
* 读取line中word Array
* 取第一个生产JSONObject对象
* 获取content
*/
Object wordObject = worldJson.getJSONArray("word").get(0);
JSONObject wordJson= JSONObject.fromObject(wordObject);
System.out.println(wordJson.get("content"));