使用fastjson时 JSON字段与Java对象字段映射

使用fastjson时 JSON字段与Java对象字段映射

package com.zto.demo;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.annotation.JSONField;



public class FastjsonTest {

	public static void main(String[] args) {
		
		//将JSON转对象
		String pieceJson = "{\"PROBLEM_NAME\":\"高伟刚\"}";
		ProblemPiece problemPiece = JSONObject.parseObject(pieceJson, ProblemPiece.class);
		System.out.println(problemPiece.getProblemName());
		
		//
		ProblemPiece piece = new ProblemPiece();
		piece.setProblemName("高伟刚");
		String jsonStr = JSONObject.toJSONString(piece);  
		System.out.println(jsonStr);
		
		
	}

	public static class ProblemPiece {

		@JSONField(name="PROBLEM_NAME")
		private String problemName;

		public String getProblemName() {
			return problemName;
		}

		public void setProblemName(String problemName) {
			this.problemName = problemName;
		}

	}

}

猜你喜欢

转载自weigang-gao.iteye.com/blog/2377862
今日推荐