如何读取json字符串中的数组? json文件中的数据读入数组
\u5982\u4f55\u8bfb\u53d6json\u5b57\u7b26\u4e32\u4e2d\u7684\u6570\u7ec4json\u6765\u505a\u5427\uff0c\u5148\u53bb\u4e0b\u8f7d\u4e0bjar\u5305\u3002\u4e0b\u9762\u662f\u6d4b\u8bd5\u7684\u4ee3\u7801\uff1a
try{ String str = "{'list':['1,2','3,4','5,6']}"; JSONObject jsonObject = JSONObject.fromObject(str); //\u5427\u8f6c\u4e3ajson\u5bf9\u8c61 String array= jsonObject.getString("list"); //\u83b7\u53d6list\u7684\u503c JSONArray jsonArray = JSONArray.fromObject(array); //\u5427list\u7684\u503c\u8f6c\u4e3ajson\u6570\u7ec4\u5bf9\u8c61 Object[] strs = jsonArray.toArray(); //json\u8f6c\u4e3a\u6570\u7ec4 for(Object s:strs){ System.out.println(s); }}catch(Exception e){e.printStackTrace();}
\u597d\u50cf\u4e0d\u80fd\u76f4\u63a5\u6253\u7f51\u9875\u5730\u5740\u3002\u4f60\u53ef\u4ee5\u53c2\u8003\u4ee5\u4e0b\u5185\u5bb9\u3002
Java \u8bfb\u5199json\u683c\u5f0f\u7684\u6587\u4ef6\u65b9\u6cd5\u8be6\u89e3
\u4e00\u3001\u8981\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u9996\u5148\u8981\u77e5\u9053json\u683c\u5f0f\u662f\u4ec0\u4e48\uff1f
JSON\u683c\u5f0f\uff1a
\u6bd4\u5982\u5b66\u751f\u6709\u5b66\u53f7\uff0c\u59d3\u540d\uff0c\u6027\u522b\u7b49\u3002
\u7528json\u8868\u793a\u5219\u4e3a\uff1a
{"studno":"11111","studname":"wwww","studsex":"\u7537"}(\u5404\u4e2a\u5b57\u6bb5\u90fd\u662f\u5b57\u7b26\u578b\uff09
\u8fd9\u4ee3\u8868\u4e00\u4e2a\u5b66\u751f\u7684\u4fe1\u606f\u3002
\u5982\u679c\u591a\u4e2a\u5462\uff1f
[{"studno":"122222","studname":"wwww","studsex":"\u7537"},
{"studno":"11111","studname":"xxxx","studsex":"\u7537"},
{"studno":"33333","studname":"ssss","studsex":"\u7537"}]
\u8fd9\u5c31\u662fjson\u683c\u5f0f\u3002
\u4e8c\u3001\u90a3\u5982\u4f55\u64cd\u4f5cjson\u683c\u5f0f\u7684\u6587\u4ef6\u5462\uff1f
\u8fd9\u4e2a\u66f4\u7b80\u5355\u4e86\uff0c\u8bf4\u767d\u4e86\u5c31\u662f\u76f4\u63a5\u8bfb\u5199\u6587\u4ef6\uff0c\u518d\u628a\u8bfb\u51fa\u6765\u7684\u6587\u4ef6\u5185\u5bb9\u683c\u5f0f\u5316\u6210json\u5c31\u53ef\u4ee5\u4e86\u3002
\u4e09\u3001\u5177\u4f53\u64cd\u4f5c\u3002
1.\u6211\u6709\u4e00\u4e2a\u5b9e\u4f53\u7c7b\uff0c\u5982\u4e0b\uff1a
public class ElectSet {
public String xueqi;
public String xuenian;
public String startTime;
public String endTime;
public int menshu;
public String isReadDB;
//{"xueqi":,"xuenian":,"startTime":,"endTime":,"renshu":,"isReadDB":}
public String getXueqi() {
return xueqi;
}
public void setXueqi(String xueqi) {
this.xueqi = xueqi;
}
public String getXuenian() {
return xuenian;
}
public void setXuenian(String xuenian) {
this.xuenian = xuenian;
}
public String getStartTime() {
return startTime;
}
public void setStartTime(String startTime) {
this.startTime = startTime;
}
public String getEndTime() {
return endTime;
}
public void setEndTime(String endTime) {
this.endTime = endTime;
}
public int getMenshu() {
return menshu;
}
public void setMenshu(int menshu) {
this.menshu = menshu;
}
public String getIsReadDB() {
return isReadDB;
}
public void setIsReadDB(String isReadDB) {
this.isReadDB = isReadDB;
}
}
2.\u6709\u4e00\u4e2ajson\u683c\u5f0f\u7684\u6587\u4ef6\uff0c\u5b58\u7684\u5c31\u662f\u4ed6\u7684\u4fe1\u606f\uff0c\u5982\u4e0b
Sets.json:
{"xuenian":"2007-2008","xueqi":"1","startTime":"2009-07-19 08:30","endTime":"2009-07-22 18:00","menshu":"10","isReadDB":"Y"}
3.\u5177\u4f53\u64cd\u4f5c.
/*
* \u53d6\u51fa\u6587\u4ef6\u5185\u5bb9\uff0c\u586b\u5145\u5bf9\u8c61
*/
public ElectSet findElectSet(String path){
ElectSet electset=new ElectSet();
String sets=ReadFile(path);//\u83b7\u5f97json\u6587\u4ef6\u7684\u5185\u5bb9
JSONObject jo=JSONObject.fromObject(sets);//\u683c\u5f0f\u5316\u6210json\u5bf9\u8c61
//System.out.println("------------" jo);
//String name = jo.getString("xuenian");
//System.out.println(name);
electset.setXueqi(jo.getString("xueqi"));
electset.setXuenian(jo.getString("xuenian"));
electset.setStartTime(jo.getString("startTime"));
electset.setEndTime(jo.getString("endTime"));
electset.setMenshu(jo.getInt("menshu"));
electset.setIsReadDB(jo.getString("isReadDB"));
return electset;
}
//\u8bbe\u7f6e\u5c5e\u6027\uff0c\u5e76\u4fdd\u5b58
public boolean setElect(String path,String sets){
try {
writeFile(path,sets);
return true;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
}
//\u8bfb\u6587\u4ef6\uff0c\u8fd4\u56de\u5b57\u7b26\u4e32
public String ReadFile(String path){
File file = new File(path);
BufferedReader reader = null;
String laststr = "";
try {
//System.out.println("\u4ee5\u884c\u4e3a\u5355\u4f4d\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\uff0c\u4e00\u6b21\u8bfb\u4e00\u6574\u884c\uff1a");
reader = new BufferedReader(new FileReader(file));
String tempString = null;
int line = 1;
//\u4e00\u6b21\u8bfb\u5165\u4e00\u884c\uff0c\u76f4\u5230\u8bfb\u5165null\u4e3a\u6587\u4ef6\u7ed3\u675f
while ((tempString = reader.readLine()) != null) {
//\u663e\u793a\u884c\u53f7
System.out.println("line " line ": " tempString);
laststr = laststr tempString;
line ;
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e1) {
}
}
}
return laststr;
}
//\u628ajson\u683c\u5f0f\u7684\u5b57\u7b26\u4e32\u5199\u5230\u6587\u4ef6
public void writeFile(String filePath, String sets) throws IOException {
FileWriter fw = new FileWriter(filePath);
PrintWriter out = new PrintWriter(fw);
out.write(sets);
out.println();
fw.close();
out.close();
}
4.\u8c03\u7528\uff0c\u4f7f\u7528\uff08\u5728\u7f51\u7ad9\u7684controller\u91cc\u8c03\u7528\u7684\uff09
//\u53d6\u51fajson\u5bf9\u8c61
public void GetElectSettings(HttpServletRequest request,
HttpServletResponse response) throws Exception {
ElectSet electset=new ElectSet();
String absPath = request.getRealPath("\");
String filePath = absPath "public\sets\electSets.json";
electset=businessService.findElectSets(filePath);//\u8fd9\u91cc\u662f\u8c03\u7528\uff0c\u5927\u5bb6\u81ea\u5df1\u6539\u6539\uff0c\u6211\u8c03\u7528\u7684\u4e1a\u52a1\u5c42 \u7684\u3002
JSONArray jsonItems = new JSONArray();
jsonItems.add(electset);
JSONObject jo=new JSONObject();
jo.put("data", jsonItems);
System.out.println(jo);
request.setCharacterEncoding("utf-8");
response.setContentType("application/json;charset=utf-8");
PrintWriter out = response.getWriter();
out.print(jo);
}
//\u4fee\u6539json\u6587\u4ef6
public void ChangeElectSet(HttpServletRequest request,
HttpServletResponse response) throws Exception {
request.setCharacterEncoding("UTF-8");
response.setContentType("text/json;charset=utf-8");
log.info("reach ChangeElectSet");
String json = (String) request.getParameter("json").trim();
log.info("Change ElectSet");
log.info(json);
ElectSet sets = new ElectSet();
JSONObject jsonObject = JSONObject.fromObject(json);
sets = (ElectSet) JSONObject.toBean(jsonObject, ElectSet.class);
if(sets.getIsReadDB()=="false"){
sets.setIsReadDB("\u5426");
}
else{
sets.setIsReadDB("\u662f");
}
String changes="{"xuenian":"";//\u56e0\u4e3ajson\u7684\u5c5e\u6027\u8981\u7528\u5f15\u53f7\uff0c\u6240\u4ee5\u8981\u7528"\u8f6c\u4e49\u4e00\u4e0b
changes =sets.getXuenian() "","xueqi":"" sets.getXueqi() "","startTime":"" sets.getStartTime() "","endTime":"" sets.getEndTime() "","menshu":"" sets.getMenshu() "","isReadDB":"" sets.getIsReadDB() ""}";
System.out.println(changes);
String absPath = request.getRealPath("\");
String filePath = absPath "public\sets\electSets.json";
json来做吧,先去下载下jar包。
下面是测试的代码:
try{
String str = "{'list':['1,2','3,4','5,6']}";
JSONObject jsonObject = JSONObject.fromObject(str); //把转为json对象。
String array= jsonObject.getString("list"); //获取list的值。
JSONArray jsonArray = JSONArray.fromObject(array); //把list的值转为json数组对象。
Object[] strs = jsonArray.toArray(); //json转为数组。
for(Object s:strs){
System.out.println(s);
}
}catch(Exception e){e.printStackTrace();}
绛旓細鏁扮粍閬嶅巻寰堢畝鍗曞晩锛屼笉杩囨ゼ涓诲簲璇ユ槸鎯冲懡鍚嶅悗閬嶅巻鍚 var data = [ [3, "1", "[email protected]", "Mac", 1, 25, 1231231233, "1"], [2, "1", "[email protected]", "Mac", 1, 25, 1231231222, "1"]], i = 0, line, o = {}, keys = ['a', 'b', 'c', 'd', 'e',...
绛旓細鍦↗avaScript涓紝瑙f瀽JSON鏁版嵁骞跺皢鍏惰浆鍖栦负鏁扮粍鐨勬搷浣滈潪甯哥洿瑙傦紝涓昏鍊熷姪浜巂eval()`鍑芥暟銆備緥濡傦紝瀵逛簬涓涓寘鍚祵濂楀璞″拰鏁扮粍鐨凧SON瀛楃涓`t`锛屾垜浠彲浠ラ氳繃浠ヤ笅姝ラ杩涜澶勭悊锛氳繖娈典唬鐮侀鍏堝畾涔変簡涓涓寘鍚鏉傜粨鏋勭殑JSON瀛楃涓诧紝鐒跺悗浣跨敤`eval()`鍑芥暟灏嗗叾杞寲涓篔avaScript瀵硅薄銆傛帴鐫锛岄氳繃寰幆閬嶅巻鏁扮粍锛屾垜浠...
绛旓細1銆佸厛瀹氫箟涓涓json鍙橀噺銆2銆佺劧鍚庡畾涔夎繖涓彉閲忕殑鍊硷紝鍏跺疄涓涓猨son瀵硅薄灏辨槸涓涓猳bj瀵硅薄銆3銆佽娣诲姞json瀵硅薄鐨勫厓绱狅紝鐩存帴浣跨敤鏁扮粍涓嬫爣鏂瑰紡锛屾坊鍔犻敭鍜屽煎氨琛屼簡銆4銆佽涓簀son瀵硅薄娣诲姞瀵硅薄锛屾垜浠彲浠ュ厛鍒涘缓涓涓璞°5銆佺劧鍚庝篃鏄娇鐢ㄦ暟缁勪笅鏍囨柟寮忥紝涓哄叾璧嬪间负鏂扮殑瀵硅薄鍙橀噺灏辫浜嗐6銆佸湪娴忚鍣閲锛屾垜浠氨鍙互...
绛旓細濂藉儚涓嶈兘鐩存帴鎵撶綉椤靛湴鍧銆備綘鍙互鍙傝冧互涓嬪唴瀹广侸ava 璇诲啓json鏍煎紡鐨勬枃浠舵柟娉曡瑙 涓銆佽瑙e喅杩欎釜闂棣栧厛瑕佺煡閬搄son鏍煎紡鏄粈涔堬紵JSON鏍煎紡锛氭瘮濡傚鐢熸湁瀛﹀彿锛屽鍚嶏紝鎬у埆绛夈傜敤json琛ㄧず鍒欎负锛歿"studno":"11111","studname":"wwww","studsex":"鐢"}(鍚勪釜瀛楁閮芥槸瀛楃鍨嬶級杩欎唬琛ㄤ竴涓鐢熺殑淇℃伅銆傚鏋...
绛旓細鍏堜娇鐢╦son_decode()鍑芥暟瑙f瀽鎴愭暟缁勶紝鍦ㄧ敤鍙橀噺绛変簬瑙f瀽鍑烘潵鐨勬暟缁 test=json_decode("json鏁版嵁");杈撳嚭 echo $test->key;
绛旓細json鏁版嵁mobile閲岄潰鍖呭惈浜唗empdata 閭d箞浣犵殑mobile瀵硅薄 閲岄潰涔熼渶瑕佸寘鍚竴涓猼empdata鐨刲ist瀵硅薄鏉ユ帴鏀 class Mobile{ List<Tempdata> tempdata; }class Tempdata{ private float t; ...}
绛旓細杩欎釜鍙互鍚son鏍煎紡鐨瀛楃涓瑙f瀽鎴鏁扮粍json_decode()鍑芥暟锛屽彉鎴愭暟缁勪互鍚庡氨鍙互鏂逛究鎿嶄綔浜嗭紝鍙互鍒犻櫎鏁扮粍涓殑浠绘剰涓椤癸紝涔熷彲浠ュ鍔犱竴椤规瘮濡傦細array_push($data,['sort'=>3,'catentryId'=>10003]),鍐嶅彉鎴恓son鏍煎紡鐨勫瓨鍏ユ暟鎹簱銆傛柟娉曟湁澶氱锛岃繖閲岀畝鍗曠殑绀轰緥涓 ...
绛旓細package com;import java.util.List;import com.Kensou.Person;import com.alibaba.fastjson.JSON;public class Yuma{public static void main(String[] args){String result = "{\"data1\":[{\"name\":\"aa\",\"age\":\"12\"},{\"name\":\"bb\",\"age\":\"13\"}],\"data2...
绛旓細1銆佸垱寤簆ython鏂囦欢锛宼estjson.py锛2銆佺紪鍐檖ython浠g爜锛岃В鏋json鏁扮粍锛宩son_str = {"name":"lisi","age":27} for key, value in json_str.items():print(str(key) + '=' + str(value))3銆佸彸鍑伙紝閫夋嫨鈥樺湪缁堢涓繍琛孭ython鏂囦欢鈥欙紱4銆佹煡鐪嬭繍琛岀粨鏋滐紝鍗冲彲鍙戠幇瑙f瀽瀹屾垚锛...
绛旓細var json={"a":"aaa","b":{"c":"ccc","d":[{"e":"01","g":"kkkk"},{"e":"07","g":"lllll"},{"e":"05","g":"xxxxx"},{"e":"03","g"