在之前的项目中,数据从后台返回到前台,一般是把bean 或List或Map转为转为JSON格式输出,用到JSONObject或者JSONArray。这个容易处理,博客中写的有。
但是,这次在远程接口调用的时候,获取的数据时JSON,但是要取值,却发现不是那么容易。当初想的是转化为bean或List或Map,这样通过get/set,put/get取值赋值。但不容易实现,难道是我引用的json jar包 不对或者是某些方法不熟悉。反正最后,一步步的通过split截取的,之后转化为Map。第二步进行进一步的细化。然后拼装成String.
应该说如果正则表达式写的好的话,也能截取出来,可惜我的正则表达式不行。
所以只好一步步的来操作截取。
第一步程序:
public static Map<String, String> getJSONobject(String str){
String sb = str.substring(1, str.length()-1);
String[] name = sb.split("\\\",\\\"");
String[] nn =null;
Map map = new HashMap();
for(int i= 0;i<name.length; i++)
{
nn = name[i].split("\\\":\\\"");
map.put(nn[0], nn[1]);
}
return map;
}
第二步程序根据需求处理字符串,符合实际要求
public static String chuliString(Map<String,String> map){
String conventionSuggestions =null;
String sportSuggestion = null;
String foodSuggestion = null;
String doctorSuggestion = null;
String advice = null;;
Iterator<Entry<String,String>> iter = map.entrySet().iterator();
while(iter.hasNext()) {
Entry<String, String> entry = iter.next();
if((entry.getKey()).equals("conventionSuggestion")){
String conventionSuggestion = entry.getValue();
conventionSuggestions = conventionSuggestion.split("}")[0];
; System.out.println("conventionSuggestions="+conventionSuggestions.substring(0, conventionSuggestions.length()-1));
}
if((entry.getKey()).equals("suggestion':{'sportSuggestion")){
sportSuggestion = entry.getValue();
System.out.println("sportSuggestion="+sportSuggestion);
}
if((entry.getKey()).equals("foodSuggestion")){
foodSuggestion = entry.getValue();
System.out.println("foodSuggestion="+foodSuggestion);
}
if((entry.getKey()).equals("doctorSuggestion")){
doctorSuggestion = entry.getValue();
System.out.println("doctorSuggestion="+doctorSuggestion);
}
}
advice = conventionSuggestions+","+sportSuggestion+","+foodSuggestion+","+doctorSuggestion;
System.out.println("advice="+advice);
return advice ;
}
第二步程序:需求不一样,逻辑也就不同,但是处理方式是一样的,可以参考一下。
分享到:
相关推荐
List<Bean>转换为List<Map>, List转换为List<Object> list转化为 JsonArray ,list转换JsonArray
json转换为String/List/Map/Bean
json,bean,map,list之间相互转换
json与bean,array,list,map,简单类型之间的封装、解析
json2bean json2xml json2list json2map
bean转json字符串,JSONArray对象转换成list集合,json字符串转换成list集合,JSONObject转换成map对象,json字符串转换成map对象等等各种方法
json_jackson_junit_demo json 与 bean list map 相互转换
JSONObject jsonObject =JSONObject.fromObject(new JsonBean()); 4. 数组转换成json代码 boolean[] boolArray = newboolean[] { true, false, true }; JSONArray jsonArray1 =JSONArray.fromObject(boolArray); ...
JSON数据以来包全部的jar包 解压后放在项目下就可以了 下面付源代码 import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import net.sf.json.JSONArray; ...
需求:需要将一个格式是[String,MonthValue]的map转换为json,这里YearValue是自定义的case class。 MonthValue长这样,这里需要加上@BeanProperty注解,否则 fastjson解析不到。 case class MonthValue(@Bean...
json和map或者bean之间的转换,可以简单的把json和map或者list或者是bean进行转换
通过fastjson实现了JavaBean,list,MAP,list等格式与json格式之间的转换,已经附带fastjson的包,包的版本为fastjson-1.1.36.jar
JSONSerializer.deserializer :将JSON文本解析为List和MapBeanSerializer.serializer :把Java Bean序列化为List和MapSeriable :把Java Bean中要序列化的字段用此注解。无此注解的字段将不序列化;ParseException :...
//Map转换成json, 是用jsonObject @Test public void testMapToJSON(){ Map map = new HashMap(); map.put( "name", "json" ); map.put( "bool", Boolean.TRUE ); map.put( "int", new Integer(1) ); ...
json工具类:一个String字符串转换为json格式、一个obj对象转换为json格式、一个bean对象转换为json格式、一个Map集合转换为json格式、json字符串转换为List
仅供学习,里面有json与list、map、bean格式相互转换的例子源代码
jackson框架解析json,主要讲解了怎样解析Bean,List<Bean>,Map,Bean>,List<Map,Bean>>类型的json
可转化JAVA对象(bean,list,map,set)。如果要求返回的JSON对象有严格的要求此包不适用,不推荐下载了。
json帮助类,json字符串转换java各种对象,如MAP、BEAN、LIST等。
支持普通JDK类包括任意Java Bean Class、Collection、Map、Date或enum 零依赖(没有依赖其它任何类库除了JDK) //Serialize List list=new ArrayList(); String json=new JsonSerializer().prettyFormat...