zoukankan      html  css  js  c++  java
  • java实体类和json串字段名称不一致或者与map中字段名称不一致使用注解转化

    package yuanCheng;
    
    import java.text.MessageFormat;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Map;
    
    import com.alibaba.fastjson.JSONObject;
    import com.alibaba.fastjson.annotation.JSONField;
    
    import yuanCheng.bean.NeedInfo;
    import yuanCheng.constant.SapConstant;
    
    public class Controller {
    
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		init("SP","S");
    	}
    	public static void init(String company, String cardType) {
    		//String url=String.format(SapConstant.PARTNER_INIT +"?Company={0}&CardType={1}",company,cardType);
    		String url = MessageFormat.format(SapConstant.PARTNER_INIT,company,cardType);//封装请求路径和请求参数
    		System.out.println("url==========="+url);
    		String res = HttpUtil.doGet(url,null);//发送远程get请求
    		Map<String,Object> result = JSONObject.parseObject(res, Map.class);//将json串转换成map类型
    		System.out.println("result====="+result);
    		Map<String,Object>data=JSONObject.parseObject(JSONObject.toJSONString(result.get("data")),Map.class);//获取data对象中的值
    		
    		List<Map<String,Object>>list=getList(data.get("placeOfDelivery"));//获取placeOfDelivery中的值转换成list
    		System.out.println("list=============="+list);
    		System.out.println("getlistValue======"+list.get(0));
    		//将map转换成实体类中的值
    		NeedInfo info=JSONObject.parseObject(JSONObject.toJSONString(list.get(0)),NeedInfo.class);//将map中的值放入实体类中,实体类名称和map字段名称不一致使用@JSONField(name="lineNum")注解转换
    		System.out.println("将名称不一致字段的值放入对应字段中info============"+info);
    		
    	}
    	//将object类型的list转换成list
    	public static List<Map<String,Object>>getList(Object obj){
    		 List<Map<String,Object>> result = new ArrayList<>();
    		    if (obj instanceof List<?>) {
    		        for (Object o : (List<?>) obj) {
    		            result.add(Map.class.cast(o));
    		        }
    		    }
    		    result.forEach(System.out::println); // 输出:1 ab
    		return result;
    	}
    
    
    }
    

      //实体类

    package yuanCheng.bean;
    
    import java.io.Serializable;
    
    import com.alibaba.fastjson.annotation.JSONField;
    public class NeedInfo implements Serializable{
    	/**
    	 * 
    	 */
    	private static final long serialVersionUID = 1L;
    	@JSONField(name="lineNum")
    	private String line;
    	@JSONField(name="value")
    	private String valueName;
    	public String getLine() {
    		return line;
    	}
    	public void setLine(String line) {
    		this.line = line;
    	}
    	public String getValueName() {
    		return valueName;
    	}
    	public void setValueName(String valueName) {
    		this.valueName = valueName;
    	}
    	@Override
    	public String toString() {
    		return "NeedInfo [line=" + line + ", valueName=" + valueName + "]";
    	}
    	
    
    }
    

      //远程调用接口util类

    package yuanCheng;
    
    import org.apache.http.client.ResponseHandler;
    import org.apache.http.client.methods.HttpGet;
    import org.apache.http.client.methods.HttpPost;
    import org.apache.http.entity.StringEntity;
    import org.apache.http.impl.client.BasicResponseHandler;
    import org.apache.http.impl.client.CloseableHttpClient;
    import org.apache.http.impl.client.HttpClients;
    
    
    /**
     * create by Dell on 2020/6/17
     */
    public class HttpUtil {
    	//get请求
    	public static String doGet(String url,String authValue){
    		String result = null;
    		CloseableHttpClient httpClient = HttpClients.createDefault();
    		ResponseHandler<String> responseHandler = new BasicResponseHandler();
    		try {
    			HttpGet httpGet = new HttpGet(url);
    			httpGet.setHeader("Content-type", "application/json");
    			if(null!=authValue){
    				httpGet.setHeader("Authorization","Bearer "+authValue);
    			}
    			result = httpClient.execute(httpGet, responseHandler);
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    		finally {
    			try {
    				httpClient.close();
    			} catch (Exception e) {
    				System.out.println(e.getMessage());
    			}
    		}
    		return result ;
    	}
    
    	// post请求参数为json格式
    	public static String doJsonPost(String url, String json,String authValue) {
    		String result = null;
    		CloseableHttpClient httpClient = HttpClients.createDefault();
    		ResponseHandler<String> responseHandler = new BasicResponseHandler();
    		try {
    			HttpPost httpPost = new HttpPost(url);
    			StringEntity requestEntity = new StringEntity(json, "utf-8");
    			requestEntity.setContentEncoding("UTF-8");
    			httpPost.setHeader("Content-type", "application/json");
    			if(null!=authValue){
    				httpPost.setHeader("Authorization","Bearer "+authValue);
    			}
    			httpPost.setEntity(requestEntity);
    			result = httpClient.execute(httpPost, responseHandler);
    		} catch (Exception e) {
    			System.out.println(e.getMessage());
    		} finally {
    			try {
    				httpClient.close();
    			} catch (Exception e) {
    				System.out.println(e.getMessage());
    			}
    		}
    		return result;
    	}
    }
    

      

  • 相关阅读:
    Linux进程实践(5) --守护进程
    Linux进程实践(4) --wait避免僵尸进程
    Linux进程实践(3) --进程终止与exec函数族
    Linux进程实践(2) --僵尸进程与文件共享
    ORACLE清除某一字段重复的数据(选取重复数据中另一个字段时期最大值)
    ORACLE查询某一字段重复的数据
    mysql字符集设置
    项目中填写全局常量
    mybatis的insert简单使用
    window.open()读取本地图片简单使用总结
  • 原文地址:https://www.cnblogs.com/xianz666/p/14003688.html
Copyright © 2011-2022 走看看