问题描述
- 求大神解析一下这个JSON
-
Value {"data":[{"school_name":"西北农林科技大学","school_id":"8"},{"school_name":"西北大学","school_id":"6"},{"school_name":"西北工业大学","school_id":"5"},{"school_name":"西北政法大学","school_id":"2"},{"school_name":"西安交通大学","school_id":"4"},{"school_name":"西安建筑科技大学","school_id":"10"},{"school_name":"西安电子科技大学","school_id":"3"},{"school_name":"西安邮电大学","school_id":"1"},{"school_name":"长安大学","school_id":"9"},{"school_name":"陕西师范大学","school_id":"7"},{"school_name":"陕西科技大学","school_id":"11"}],"msg":"列表获取成功","status":0}
用2个数组接收school_name和school_id
用java里的方法
解决方案
//第一次解析;
public Map parseStatus(String JSONStr) {
JSONArray jsonArray = JSONArray.fromObject(JSONStr);
int n = jsonArray.size();
Map map = new HashMap();
for (int i = 0; i < n; i++) {
JSONObject obj = jsonArray.getJSONObject(i);
if (obj == null)
continue;
if (obj.containsKey("data")) {
map.put("data", obj.getString("data"));
}
if (obj.containsKey("status")) {
map.put("status", obj.getString("status"));
}
if(obj.containsKey("msg")){
map.put("msg", obj.getString("msg"));
}
}
return map;
}
//拿到map中data;
String JSONSchool=map.get("data").toString();
public List parseschool(String JSONSchool){
List schoolVoList=new ArrayList();
JSONArray jsonArray = JSONArray.fromObject(JSONStr);
int n = jsonArray.size();
for (int i = 0; i < n; i++) {
JSONObject obj = jsonArray.getJSONObject(i);
SchoolVo schoolVo=new SchoolVo();
if (obj == null)
continue;
if (obj.containsKey("school_name")) {
schoolVo.setF_jht_name(obj.getString("school_name"));
}
if (obj.containsKey("school_id")) {
schoolVo.setF_jht_id(obj.getInt("school_id"));
}
schoolVoList.add(schoolVo);
}
return schoolVoList;
}
解决方案二:
楼上答的很漂亮啊
解决方案三:
使用json-lib-2.2.2-jdk15.jar进行JSON解析。示例代码:可直接运行,需要jar可百度或者联系我。
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
public class JSONParse {
public static void main(String[] args) {
String value = read("src/json.txt",null);
Map<String,String> map = parseSchoolInfo(value);
if(map!=null){
System.out.println(map.toString());
}else{
System.out.println("no data parsed.");
}
}
public static Map<String,String> parseSchoolInfo(String json){
if(json==null||"".equals(json)){
System.out.println("empty json.");
return null;
}
JSONObject object = JSONObject.fromObject(json);
if(object==null||!object.containsKey("data")){
System.out.println("没有数据信息.");
return null;
}
if(!object.containsKey("status")||object.getInt("status")!=0){
System.out.println("信息状态错误.");
return null;
}
JSONArray schoolArray = object.getJSONArray("data");
if(schoolArray==null||schoolArray.size()==0){
System.out.println("没有解析到数据信息");
return null;
}
//最终遍历data中的JSON数组解析存入map
Map<String,String>schools = new HashMap<String,String>();
for(int i = 0;i<schoolArray.size();i++){
JSONObject school = schoolArray.getJSONObject(i);
String school_name = school.getString("school_name");
String school_id = school.getString("school_id");
schools.put(school_id, school_name);
}
return schools;
}
/**
* 读取指定文件的文本内容,返回文本值是一个长串,原文本中的换行不会被读入
* @param fileName 文件名称
* @return
*/
public static String read(String fileName,String charSet){
if(fileName==null||"".equals(fileName)){
System.out.println("文件名称为空...");
return "";
}
//设置字符编码
if(charSet==null||"".equals(charSet)){
charSet = "UTF-8";
}
File file = new File(fileName);
FileInputStream fin = null;
StringBuffer buffer = new StringBuffer();
try {
fin = new FileInputStream(file);
BufferedReader reader = new BufferedReader(new InputStreamReader(fin,charSet));
String content = reader.readLine();
while(content!=null){
buffer.append(content);
//循环读取下一行,没有这行代码就是死循环的啦。。。
content = reader.readLine();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
if(fin!=null){
try {
fin.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return buffer.toString();
}
}
解决方案四:
{data:
[
{
"school_name":"西北农林科技大学",
"school_id":"8"
}
]
}
看清楚结构了吗?
1,先创建一个类Info,就两个属性,school_name,school_id
2,用gson直接将json转换成List,完事儿。。。