json在线等,鄙人研究了很长时间,没有进展 json对象 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 推荐Gson(一个处理json的jar包)如果没有,下载地址http://download.csdn.net/detail/u014772349/7899045以后为解析的代码仅供参考import java.io.BufferedReader;import java.io.File;import java.io.FileInputStream;import java.io.IOException;import java.io.InputStreamReader;import com.google.gson.Gson;import com.google.gson.internal.StringMap;public class Test { /** * @param args * @throws IOException */ public static void main(String[] args) throws IOException { StringBuffer sbf = new StringBuffer(); BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream( "F:"+File.separator+"json.txt"),"UTF-8")); //json字符串 String str=""; while((str=reader.readLine())!=null){ sbf.append(str); } reader.close(); StringMap strMap=parseJson(sbf.toString()); //把StringMap转为你自己的VO System.out.println(strMap); } public static StringMap parseJson(String jsonstr){ Gson gson = new Gson(); StringMap strMapArray=gson.fromJson(jsonstr, StringMap.class); return strMapArray; } } VO:汽油ID(可选) 汽油类型 汽油型号 汽油价格 汽油优惠 恩 ,差不多就这么多了。 {"A":{"A95":{"price":666,"rebate":10},A92:{"price":888,"rebate":20}},"B":{"B0":{"price":454,"rebate":3454}}} 这组json字符串的vo对象是什么,帮忙看看,我写了个vo,但是数据获取不到数据。 {"A":{"A95":{"price":666,"rebate":10},A92:{"price":888,"rebate":20}},"B":{"B0":{"price":454,"rebate":3454}}} 这组json字符串的vo对象是什么,帮忙看看,我写了个vo,但是数据获取不到数据。这是写的一个vo对象 import java.io.BufferedReader;import java.io.File;import java.io.FileInputStream;import java.io.IOException;import java.io.InputStreamReader;import java.util.ArrayList;import java.util.Iterator;import java.util.List;import java.util.Map;import com.google.gson.Gson;import com.google.gson.internal.StringMap;public class Test { /** * @param args * @throws IOException */ public static void main(String[] args) throws IOException { //json字符串 String str=getJsonStr(); //把json字符串转为自己的VO列表 List<VO> list = convertJsonToList(str); //重新输出JSON(实际上组装为下面的JSON比较方面) Gson gson = new Gson(); System.out.println(gson.toJson(list)); //[{"type":"A95","pidtype":"A","price":"666.0","rebate":"10.0"}, //{"type":"A92","pidtype":"A","price":"888.0","rebate":"20.0"}, //{"type":"B0","pidtype":"B","price":"454.0","rebate":"3454.0"}] } /** * 获得要转换的JSON字符串<br> * 这边为了方面我直接读的我本地的文件内容为你要转换的json字符串 * @return * @throws IOException */ private static String getJsonStr() throws IOException{ StringBuffer sbf = new StringBuffer(); BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream( "F:"+File.separator+"json.txt"),"UTF-8")); //json字符串 String str=""; while((str=reader.readLine())!=null){ sbf.append(str); } reader.close(); return sbf.toString(); } private static StringMap parseJson(String jsonstr){ Gson gson = new Gson(); StringMap strMapArray=gson.fromJson(jsonstr, StringMap.class); return strMapArray; } /** * 把json字符串转为自己的VO列表 * @param str * @return * @throws ClassNotFoundException * @throws IllegalAccessException * @throws InstantiationException */ private static List<VO> convertJsonToList(String str ) { Map<String,Map<String,Map<String,Object>>> map = parseJson(str); List<VO> list = new ArrayList<VO>(); Iterator<String> iterator = map.keySet().iterator(); while(iterator.hasNext()){ String firstkey = iterator.next(); Map<String,Map<String,Object>> voMap= map.get(firstkey); Iterator<String> iteratorVO = voMap.keySet().iterator(); while(iteratorVO.hasNext()){ String key = iteratorVO.next(); Map<String,Object> map1 = voMap.get(key); VO vo = new VO(); vo.setType(key); vo.setPidtype(firstkey); vo.setPrice(""+map1.get("price")); vo.setRebate(""+map1.get("rebate")); list.add(vo); } } return list; } }class VO { private String type;//当前油的种类 private String pidtype;//当前油的所属的种类(汽油/柴油) private String price;//价格 private String rebate;//优惠信息 public String getPrice() { return price; } public void setPrice(String price) { this.price = price; } public String getRebate() { return rebate; } public void setRebate(String rebate) { this.rebate = rebate; } public String getType() { return type; } public void setType(String type) { this.type = type; } public String getPidtype() { return pidtype; } public void setPidtype(String pidtype) { this.pidtype = pidtype; }} A:import java.io.BufferedReader;import java.io.File;import java.io.FileInputStream;import java.io.IOException;import java.io.InputStreamReader;import java.util.ArrayList;import java.util.Iterator;import java.util.List;import java.util.Map;import com.google.gson.Gson;import com.google.gson.internal.StringMap;public class Test { /** * @param args * @throws IOException */ public static void main(String[] args) throws IOException { //json字符串 String str=getJsonStr(); //把json字符串转为自己的VO列表 List<VO> list = convertJsonToList(str); //重新输出JSON(实际上组装为下面的JSON比较方面) Gson gson = new Gson(); System.out.println(gson.toJson(list)); //[{"type":"A95","pidtype":"A","price":"666.0","rebate":"10.0"}, //{"type":"A92","pidtype":"A","price":"888.0","rebate":"20.0"}, //{"type":"B0","pidtype":"B","price":"454.0","rebate":"3454.0"}] } /** * 获得要转换的JSON字符串<br> * 这边为了方面我直接读的我本地的文件内容为你要转换的json字符串 * @return * @throws IOException */ private static String getJsonStr() throws IOException{ StringBuffer sbf = new StringBuffer(); BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream( "F:"+File.separator+"json.txt"),"UTF-8")); //json字符串 String str=""; while((str=reader.readLine())!=null){ sbf.append(str); } reader.close(); return sbf.toString(); } private static StringMap parseJson(String jsonstr){ Gson gson = new Gson(); StringMap strMapArray=gson.fromJson(jsonstr, StringMap.class); return strMapArray; } /** * 把json字符串转为自己的VO列表 * @param str * @return * @throws ClassNotFoundException * @throws IllegalAccessException * @throws InstantiationException */ private static List<VO> convertJsonToList(String str ) { Map<String,Map<String,Map<String,Object>>> map = parseJson(str); List<VO> list = new ArrayList<VO>(); Iterator<String> iterator = map.keySet().iterator(); while(iterator.hasNext()){ String firstkey = iterator.next(); Map<String,Map<String,Object>> voMap= map.get(firstkey); Iterator<String> iteratorVO = voMap.keySet().iterator(); while(iteratorVO.hasNext()){ String key = iteratorVO.next(); Map<String,Object> map1 = voMap.get(key); VO vo = new VO(); vo.setType(key); vo.setPidtype(firstkey); vo.setPrice(""+map1.get("price")); vo.setRebate(""+map1.get("rebate")); list.add(vo); } } return list; } }class VO { private String type;//当前油的种类 private String pidtype;//当前油的所属的种类(汽油/柴油) private String price;//价格 private String rebate;//优惠信息 public String getPrice() { return price; } public void setPrice(String price) { this.price = price; } public String getRebate() { return rebate; } public void setRebate(String rebate) { this.rebate = rebate; } public String getType() { return type; } public void setType(String type) { this.type = type; } public String getPidtype() { return pidtype; } public void setPidtype(String pidtype) { this.pidtype = pidtype; }}漂亮! 实体和json 的字符串名称对应好,然后映射。 struts2中Action级别的资源文件无效 求助:XML文件不能自动加载,在线等 struts国际化问题 有谁在tomcat6中运行成功过jsf1.2吗? 关于struts2页面传递参数的问题 ibatis的问题 wsdd文件能否自动生成 关于Apache和Tomcat的整合问题,谢谢! 散分!讨论《sun官方提出EJB是J2EE之败笔》 关于Eelipse插件安links装问题 迅雷下载文件显示名称问题 用js获得文本域中内容
如果没有,下载地址http://download.csdn.net/detail/u014772349/7899045以后为解析的代码仅供参考
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;import com.google.gson.Gson;
import com.google.gson.internal.StringMap;public class Test { /**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException { StringBuffer sbf = new StringBuffer();
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream( "F:"+File.separator+"json.txt"),"UTF-8"));
//json字符串
String str="";
while((str=reader.readLine())!=null){
sbf.append(str);
}
reader.close();
StringMap strMap=parseJson(sbf.toString());
//把StringMap转为你自己的VO
System.out.println(strMap);
}
public static StringMap parseJson(String jsonstr){
Gson gson = new Gson();
StringMap strMapArray=gson.fromJson(jsonstr, StringMap.class);
return strMapArray;
}
}
{"A":{"A95":{"price":666,"rebate":10},A92:{"price":888,"rebate":20}},"B":{"B0":{"price":454,"rebate":3454}}} 这组json字符串的vo对象是什么,帮忙看看,我写了个vo,但是数据获取不到数据。
{"A":{"A95":{"price":666,"rebate":10},A92:{"price":888,"rebate":20}},"B":{"B0":{"price":454,"rebate":3454}}} 这组json字符串的vo对象是什么,帮忙看看,我写了个vo,但是数据获取不到数据。
这是写的一个vo对象
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;import com.google.gson.Gson;
import com.google.gson.internal.StringMap;public class Test { /**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException { //json字符串
String str=getJsonStr();
//把json字符串转为自己的VO列表
List<VO> list = convertJsonToList(str);
//重新输出JSON(实际上组装为下面的JSON比较方面)
Gson gson = new Gson();
System.out.println(gson.toJson(list));
//[{"type":"A95","pidtype":"A","price":"666.0","rebate":"10.0"},
//{"type":"A92","pidtype":"A","price":"888.0","rebate":"20.0"},
//{"type":"B0","pidtype":"B","price":"454.0","rebate":"3454.0"}] }
/**
* 获得要转换的JSON字符串<br>
* 这边为了方面我直接读的我本地的文件内容为你要转换的json字符串
* @return
* @throws IOException
*/
private static String getJsonStr() throws IOException{
StringBuffer sbf = new StringBuffer();
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream( "F:"+File.separator+"json.txt"),"UTF-8"));
//json字符串
String str="";
while((str=reader.readLine())!=null){
sbf.append(str);
}
reader.close();
return sbf.toString();
}
private static StringMap parseJson(String jsonstr){
Gson gson = new Gson();
StringMap strMapArray=gson.fromJson(jsonstr, StringMap.class);
return strMapArray;
}
/**
* 把json字符串转为自己的VO列表
* @param str
* @return
* @throws ClassNotFoundException
* @throws IllegalAccessException
* @throws InstantiationException
*/
private static List<VO> convertJsonToList(String str ) {
Map<String,Map<String,Map<String,Object>>> map = parseJson(str);
List<VO> list = new ArrayList<VO>();
Iterator<String> iterator = map.keySet().iterator();
while(iterator.hasNext()){
String firstkey = iterator.next();
Map<String,Map<String,Object>> voMap= map.get(firstkey);
Iterator<String> iteratorVO = voMap.keySet().iterator();
while(iteratorVO.hasNext()){
String key = iteratorVO.next();
Map<String,Object> map1 = voMap.get(key);
VO vo = new VO();
vo.setType(key);
vo.setPidtype(firstkey);
vo.setPrice(""+map1.get("price"));
vo.setRebate(""+map1.get("rebate"));
list.add(vo);
}
}
return list;
}
}class VO {
private String type;//当前油的种类
private String pidtype;//当前油的所属的种类(汽油/柴油)
private String price;//价格
private String rebate;//优惠信息
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
public String getRebate() {
return rebate;
}
public void setRebate(String rebate) {
this.rebate = rebate;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getPidtype() {
return pidtype;
}
public void setPidtype(String pidtype) {
this.pidtype = pidtype;
}
}
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;import com.google.gson.Gson;
import com.google.gson.internal.StringMap;public class Test { /**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException { //json字符串
String str=getJsonStr();
//把json字符串转为自己的VO列表
List<VO> list = convertJsonToList(str);
//重新输出JSON(实际上组装为下面的JSON比较方面)
Gson gson = new Gson();
System.out.println(gson.toJson(list));
//[{"type":"A95","pidtype":"A","price":"666.0","rebate":"10.0"},
//{"type":"A92","pidtype":"A","price":"888.0","rebate":"20.0"},
//{"type":"B0","pidtype":"B","price":"454.0","rebate":"3454.0"}] }
/**
* 获得要转换的JSON字符串<br>
* 这边为了方面我直接读的我本地的文件内容为你要转换的json字符串
* @return
* @throws IOException
*/
private static String getJsonStr() throws IOException{
StringBuffer sbf = new StringBuffer();
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream( "F:"+File.separator+"json.txt"),"UTF-8"));
//json字符串
String str="";
while((str=reader.readLine())!=null){
sbf.append(str);
}
reader.close();
return sbf.toString();
}
private static StringMap parseJson(String jsonstr){
Gson gson = new Gson();
StringMap strMapArray=gson.fromJson(jsonstr, StringMap.class);
return strMapArray;
}
/**
* 把json字符串转为自己的VO列表
* @param str
* @return
* @throws ClassNotFoundException
* @throws IllegalAccessException
* @throws InstantiationException
*/
private static List<VO> convertJsonToList(String str ) {
Map<String,Map<String,Map<String,Object>>> map = parseJson(str);
List<VO> list = new ArrayList<VO>();
Iterator<String> iterator = map.keySet().iterator();
while(iterator.hasNext()){
String firstkey = iterator.next();
Map<String,Map<String,Object>> voMap= map.get(firstkey);
Iterator<String> iteratorVO = voMap.keySet().iterator();
while(iteratorVO.hasNext()){
String key = iteratorVO.next();
Map<String,Object> map1 = voMap.get(key);
VO vo = new VO();
vo.setType(key);
vo.setPidtype(firstkey);
vo.setPrice(""+map1.get("price"));
vo.setRebate(""+map1.get("rebate"));
list.add(vo);
}
}
return list;
}
}class VO {
private String type;//当前油的种类
private String pidtype;//当前油的所属的种类(汽油/柴油)
private String price;//价格
private String rebate;//优惠信息
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
public String getRebate() {
return rebate;
}
public void setRebate(String rebate) {
this.rebate = rebate;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getPidtype() {
return pidtype;
}
public void setPidtype(String pidtype) {
this.pidtype = pidtype;
}
}
漂亮!