精华内容
下载资源
问答
  • 使用fastjson处理json和jsonArray格式的数据 package com.xin.tools; import java.util.ArrayList; import java.util.HashMap; import java.util.Map; import com.alibaba.fastjson.JSON; import ...

    使用fastjson处理json和jsonArray格式的数据

    
    package com.xin.tools;
    
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.Map;
    
    import com.alibaba.fastjson.JSON;
    import com.alibaba.fastjson.JSONArray;
    import com.alibaba.fastjson.JSONObject;
    
    
    
    public class JSONTools {
    	
    	
    	/**
    	 * 功能:将json串转换为一个<String,Object>类型的map,但是只能进行简单的转换,对于包含JSONArray的数据不做特殊处理;
    	 * 
    	 * */
    	public static Map<String, Object> jsontomap(String resultline){
    		Map<String, Object> resultmap = new HashMap<String,Object>();
    		
    		//将字符串转换为JSON对象
    		JSONObject jsonObject = JSONObject.parseObject(resultline);
    		
    		//强制转换对象属性,转换为Map类型,适用于没有JSONArray类型的json串
    		resultmap = (Map<String,Object>)jsonObject;
    		
    //		for(String key : resultmap.keySet()){
    //			System.out.println(key + ":" + resultmap.get(key));
    //		}
    		
    		return resultmap;
    	}
    	
    	
    	/**
    	 *功能:将JSONAarry类型的json数据转化为一个list,list中存储的是一系列map<String,Object>容器;
    	 * 
    	 *
    	 * */
    	
    	public static ArrayList<Map<String,Object>>  jsonArraytoMap(JSONArray dataObject){
    
    		ArrayList<Map<String,Object>> datalist = new ArrayList<Map<String,Object>>();
    		
    //		List datalist = new ArrayList();
    		
    		//循环JSONArray对象数据
    		for(Object object : dataObject){
    			
    			JSONObject jsonObject = (JSONObject)object;
    			HashMap map = new HashMap<String,String>();
    			
    			//循环JSONArray中某一json串,并将json转换为map
    			//entrySet()方法返回一个set视图(Set<Map.Entry<K,V>>),遍历set视图中的元素;
    			for(Map.Entry entry : jsonObject.entrySet()){
    				
    //				if(entry.getValue() instanceof JSONArray){
    //					
    //					map.put((String)entry.getKey(),jsonArraytoMap((JSONArray)entry.getValue()));
    //				}
    //				else{
    //					
    //					map.put((String)entry.getKey(),entry.getValue());
    //				}
    				map.put((String)entry.getKey(),entry.getValue());
    			}
    			
    			//将map加入到list中
    			datalist.add(map);
    
    		}
    		
    		
    //		datalist =(ArrayList<Map<String, String>>) dataObject;
    		
    //		for(Map<String,Object> tempMap :datalist ){
    //			System.out.println(tempMap);
    //		}
    //		
    		return datalist;	
    		
    	}
    	
    	
    	public static void main(String[] args){
    		
    		String urlline = "http://feature-icbc.api.fin.ceshi.youxinjinrong.com/car_half/bank/list";
    		Map<String,String> param = new HashMap<String,String>();
    		param.put("county_id", "6670");
    		param.put("card_no", "");
    		param.put("debug_token", "58e98899beea1bfaedcb5046414dd7e9");
    		param.put("debug_show", "0");
    		
    		String s = ConnectionURL.sendGET(urlline, param);
    		System.out.println(s);
    		
    		Map<String, Object> returnMap = new HashMap<String,Object>();
    		returnMap = JSONTools.jsontomap(s);
    		
    		JSONTools.jsonArraytoMap((JSONArray)returnMap.get("data"));
    		
    		
    	}
    	
    
    }
    
    
    展开全文
  • jQuery对JSON和JSONArray的each循环

    千次阅读 2018-11-27 08:58:32
    在JS中,要对JSON和JSONArray进行循环,可以使用jQuery的each来执行,代码如下: &lt;html&gt; &lt;head&gt; &lt;meta charset="UTF-8"&gt; &lt;/head&gt; &lt;...

    在JS中,要对JSON和JSONArray进行循环,可以使用jQuery的each来执行,代码如下:

    <html>
    <head>
    	<meta charset="UTF-8">
    </head>
    <script src="jquery-1.11.3.min.js"></script>
    <script>
    window.onload = function(){
    	var oJson = {'a':'1','b':'2'};
    	$.each(oJson, function(key, value) {
    		console.log(key);
    		console.log(value);
    	});
    	var aJson = [{'a':'1'},{'b':'2'}];
    	$.each(aJson, function(index, value) {
    		console.log(index);
    		console.log(JSON.stringify(value));
    	});
    }
    </script>
    </html>

    执行结果如下:

    可以看出:

    当遍历类型为JSON时,function的第一个参数是JSON的key,第二个参数是JSON的value;

    当遍历类型为JSONArray时,function的第一个参数是JSONArray的index,第二个参数是对应位置上的JSON字符串。

    注:

    若遍历类型是JSONArray,上面的遍历也可以写成以下的格式(该方法对JSON字符串使用,不能分离key和value,获取的index为0,value就是原字符串,即被认为是一个长度为1的JSONArray)。

    	$(aJson).each(function(index, value) {
    		console.log(index);
    		console.log(JSON.stringify(value));
    	});

     

    展开全文
  • jackson解析json ObjectMapper mapper = new ObjectMapper(); HttpPrintResponse response = mapper.readValue(json, HttpPrintResponse....jackson 解析jsonarray TypeReference<List<HttpPrintResponseIte...

    jackson解析json

    ObjectMapper mapper = new ObjectMapper();
    HttpPrintResponse response = mapper.readValue(json, HttpPrintResponse.class);
    

    jackson 解析jsonarray

    TypeReference<List<HttpPrintResponseItem>> type = new TypeReference<List<HttpPrintResponseItem>>() {};
    List<HttpPrintResponseItem> list = mapper.readValue(json, type);
    

    jackson 解析,因字段不存在产生测错误处理

    错误信息

    com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "result" (class com.jasper.HttpPrintResponse), not marked as ignorable (3 known properties: , "data", "returnCode", "info"])
     at [Source: java.io.StringReader@7f234f85; line: 1, column: 15] (through reference chain: com.jasper.HttpPrintResponse["result"])
    	at com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException.from(UnrecognizedPropertyException.java:79)
    	at com.fasterxml.jackson.databind.DeserializationContext.reportUnknownProperty(DeserializationContext.java:579)
    	at com.fasterxml.jackson.databind.deser.std.StdDeserializer.handleUnknownProperty(StdDeserializer.java:672)
    	at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownProperty(BeanDeserializerBase.java:906)
    	at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:328)
    	at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:121)
    	at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:2793)
    	at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:1943)
    	at com.yianju.printer.PrinterClient.execute(PrinterClient.java:156)
    	at com.yianju.printer.PrinterClient.access$0(PrinterClient.java:136)
    	at com.yianju.printer.PrinterClient$1.run(PrinterClient.java:119)
    	at java.util.TimerThread.mainLoop(Timer.java:555)
    	at java.util.TimerThread.run(Timer.java:505)
    

    解决办法

    ObjectMapper mapper = new ObjectMapper();
    mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    

    jackson 时间处理的错误处理

    异常信息

    com.fasterxml.jackson.databind.exc.InvalidFormatException: Can not construct instance of java.util.Date from String value '2019-10-21 14:45:20': not a valid representation (error: Failed to parse Date value '2019-10-21 14:45:20': Can not parse date "2019-10-21 14:45:20": not compatible with any of standard forms ("yyyy-MM-dd'T'HH:mm:ss.SSSZ", "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", "EEE, dd MMM yyyy HH:mm:ss zzz", "yyyy-MM-dd"))
     at [Source: java.io.StringReader@48957e9d; line: 1, column: 1890] (through reference chain: com.jasper.HttpPrintResponseItem["createTime"])
    	at com.fasterxml.jackson.databind.exc.InvalidFormatException.from(InvalidFormatException.java:55)
    	at com.fasterxml.jackson.databind.DeserializationContext.weirdStringException(DeserializationContext.java:650)
    	at com.fasterxml.jackson.databind.deser.std.StdDeserializer._parseDate(StdDeserializer.java:577)
    	at com.fasterxml.jackson.databind.deser.std.DateDeserializers$DateBasedDeserializer._parseDate(DateDeserializers.java:142)
    	at com.fasterxml.jackson.databind.deser.std.DateDeserializers$DateDeserializer.deserialize(DateDeserializers.java:231)
    	at com.fasterxml.jackson.databind.deser.std.DateDeserializers$DateDeserializer.deserialize(DateDeserializers.java:214)
    	at com.fasterxml.jackson.databind.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:375)
    	at com.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeAndSet(MethodProperty.java:98)
    	at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:308)
    	at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:121)
    	at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:226)
    	at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:203)
    	at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:23)
    	at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:2793)
    	at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:1950)
    	at com.yianju.printer.PrinterClient.execute(PrinterClient.java:163)
    	at com.yianju.printer.PrinterClient.access$0(PrinterClient.java:136)
    	at com.yianju.printer.PrinterClient$1.run(PrinterClient.java:119)
    	at java.util.TimerThread.mainLoop(Timer.java:555)
    	at java.util.TimerThread.run(Timer.java:505)
    

    解决办法

    DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
    ObjectMapper mapper = new ObjectMapper();
    mapper.setDateFormat(fmt);
    
    展开全文
  • 这是java使用json所用到的相关jar包 ...JSONObject json=new JSONObject();...是定义 实例化 json ...JSONArray jsonArray = new JSONArray(); 是定义实例化json数组  json是以大括号
    https://yunpan.cn/cxbmxzW9zVg3f  访问密码 ccf9 
    
    这是java使用json所用到的相关jar包

    JSONObject json=new JSONObject(); 
    是定义 实例化 json
    JSONArray  jsonArray = new  JSONArray ();
    是定义实例化json数组
     json是以大括号开始:{}
    json . put ( key,value );
    添加数据 
    json数组是以中括号开始:[]
    jsonArray.add(json);  
    添加数据

    json格式
    { "firstName": "Frank", "lastName": "Peretti", "genre": "christian fiction" }
    json数组格式

    [

    { "firstName": "Isaac", "lastName": "Asimov", "genre": "science fiction" },

    { "firstName": "Tad", "lastName": "Williams", "genre": "fantasy" },

    { "firstName": "Frank", "lastName": "Peretti", "genre": "christian fiction" }

    ]


    组合格式

    { "programmers": [

    { "firstName": "Brett", "lastName":"McLaughlin", "email": "aaaa" },

    { "firstName": "Jason", "lastName":"Hunter", "email": "bbbb" },

    { "firstName": "Elliotte", "lastName":"Harold", "email": "cccc" }

    ],

    "authors": [

    { "firstName": "Isaac", "lastName": "Asimov", "genre": "science fiction" },

    { "firstName": "Tad", "lastName": "Williams", "genre": "fantasy" },

    { "firstName": "Frank", "lastName": "Peretti", "genre": "christian fiction" }

    ],

    "musicians": [

    { "firstName": "Eric", "lastName": "Clapton", "instrument": "guitar" },

    { "firstName": "Sergei", "lastName": "Rachmaninoff", "instrument": "piano" }

    ] }



    展开全文
  • JSONObject json=new JSONObject(); json.put("car_name","奔驰"); JSONObject json=new JSONObject(); PrintWriter out = response.getWriter(); out.append(Json.toString());
  • Java对json和jsonarray字符串的解析

    千次阅读 2018-09-11 17:10:11
    json 就是键值对关系,Map很像,两者都可以嵌套其他键值对关系或者其他数据结构。 1.举个简单 json 字符串例子 String jsonStr = "{" + "\"list\":[\"第一条数据\",\&...
  • net.sf.json.JSONArrayJSON类所需要的jar包组合。包括JSONObject等
  • org.json.JSONArray转换为int数组.pdf
  • JsonJsonArray 字段中指定字段做 like 模糊查询,在度娘的答案中辗转了许久,发现类似的提问很多,但很多都是千篇一律,牛头不对马嘴的无效复制文,因为项目需要,结合度娘某些类似答案思路自己的多次尝试,...
  • org.json.jsonArray遍历取值 JSONArray json = [{...},{...},{...}]; for (int i = 0; i < json.length(); i++) { JSONObject jsonObj =json.getJSONObject(i); String id = jsonObj.getString("ID"); ...
  • jsonJSONArray 报,net.sf.json.JSONException: A JSONArray text must start with ‘[’ at character 服务器端代码片. @RequestMapping("/filmCategotydelete") public ResultVO filmCategotydelete(@...
  • org.json.JSONArray#remove

    2015-10-30 14:03:13
    Call requires API level 19 : org.json.JSONArray#remove under API:19 can use public static JSONArray RemoveJSONArray( JSONArray jarray,int pos) { JSONArray Njarray=new JSONArray(); try{ for(int i=0;
  • 因业务需求,需要用到org.json包下的JSONArray,于是乎就有了这篇记录 =.= 废话不多说,直接上代码 import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import java.util....
  • javaScript定义json创建JSONArray

    千次阅读 2018-10-31 20:14:44
    定义JSON对象 var aaa = [];  var data={};//定义一个json对象   data.name = "";//添加键值对数据  aaa.push(data);//将json数据填入数组  JSON.stringify(aaa);//json数组转为json对象存储 ...
  • org.json.JSONArray转换为int数组

    万次阅读 2018-02-25 13:35:33
    首先,org.json.JSONArray转换为数组这一需求相信很多人是遇不到的,我也是脑抽了才有这个需求,先讲一下为啥我会有这个需求吧; 我在写java后台,前后台数据传输用的json格式,前台传给后台的一个参数值的格式是...
  • org.json.JSONObject与org.json.JSONArray

    千次阅读 2018-06-27 10:04:25
    import org.json.JSONArray;import org.json.JSONObject; public class JSONTest{ public static void main(String[] args){ //JSONObject String jsonMessage = "{\"语文\":\"88\&...
  • org.json.JSONArray array = new org.json.JSONArray(); for (int i = 0; i <selectedList.size() ; i++) { org.json.JSONObject obj = new org.json.JSONObject(); try { obj.put("id", select...
  • JSONArray array= JSONArray.parseArray(JSON.toJSONString(list)); 2.fastjson---JSONArray转List JSONArray array = new JSONArray(); List<EventColAttr> list = JSONObject.parseA.
  • springboot web项目发布页面显示net/sf/json/JSONArray解决 访问servlet是报 java.lang.NoClassDefFoundError: net/sf/json/JSONArray的错误,相关的json的jar包都已经添加至配置路径了依旧报错,原因是没有将这些...
  • 这段时间工作用到json比较多,于是系统的将net.sf.json.JSONArray与net.sf.json.JSONObject中的方法学习了一遍,下面贴出代码,其中用问号标注出来的就是暂时没有懂的地方(后面改进),有不足的地方还请指出谢谢,...
  • net.sf.json.JSONArray 转换的Json 在非Ajax中如何转换成js对象
  • Not a primitive array: class org.json.JSONArray 报错语句: JSONArray okJsonArray = new JSONArray(jsonParam.get(&quot;&quot;)); 解决办法: JSONArray okJsonArray = new JSONArray(jsonParam.get(&...
  • JSONArray json = new JSONArray(); JSONObject object = null; int total = 0; while (rs.next()) { object = new JSONObject(); object.put("state", "暂缺"); ...
  • Springboot的web开发,已经导入了pom依赖,但是无法导入包import net.sf.json.JSONArray; &lt;dependency&gt; &lt;groupId&gt;net.sf.json-lib&lt;/groupId&gt; &lt;artifactId&...
  • 一、String-->JSONArray-->List String str=""; JSONArray jsonArray = JSONArray.fromObject(str); List<String> list = (List)JSONArray.toCollection(jsonArray,Website.class);...JSONAr...

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 55,515
精华内容 22,206
关键字:

json和jsonarray