1、添加依赖
使用 阿里云仓库 http://maven.aliyun.com/nexus/#welcome
maven 配置阿里云仓库: http://blog.csdn.net/only_wan/article/details/52975698
net.sf.json-lib
json-lib
2.4
jdk15
2.操作数据(java后端)
(1)接收其他接口回调回来的数据,并从json数据中提取想要的值
System.out.println(LastResult);
JSONObject jsonObject = JSONObject.fromObject(LastResult);
//通过getString("")分别取出里面的信息
String respResult = jsonObject.getString("result");
System.out.println("respResult:"+respResult);
String[] strs = respResult.split("[\"]");
System.out.println("text=" + strs[1]);//strs[1]就是取得JSON里的result数据
结果:
{"corpus_no":"6584636619263207358","err_msg":"success.","err_no":0,"result":["北京科技馆,"],"sn":"855965987821533105182"}
respResult:["北京科技馆,"]
text=北京科技馆,
(2)将变量json字符串转成json对象
String str = "{\"result\":\""+strs[1]+"\",\"text\":\""+text+"\"}";
JSONObject Object = JSONObject.fromObject(str);
System.out.println(str);
结果:
{"result":"北京科技馆,","text": "具体地址在哪里?"}
然后将json对象放进response
// ------↓返回值给小程序↓------ //
response.setContentType("application/json");
response.setCharacterEncoding("utf-8");
PrintWriter printWriter = response.getWriter();
printWriter.write(Object.toString());
printWriter.flush();
3.微信小程序端
提取数据的写法:
success: function (res) {
// success 以下是提取非json数据的写法
var msg = res.data;
console.log('begin');
console.log(msg);
console.log(msg['result']);
console.log(msg['result'][0]);
//-----------以下是提取json数据的写法
var json1 = JSON.parse(res.data);
console.log(json1);
console.log(json1['result']);
结果:
begin
{"result":"北京科技馆,","text":"具体地址在哪里?"}
undefined
{result: "北京科技馆,", text: "具体地址在哪里?"}
北京科技馆,