fast-json.jar的使用方法

fast-json.jar 解析json数据:一种json数据解析方式是这样的,点击这里下载jsonfast.jar+fastjsonAPI文档

  1. [  
  2.     {  
  3.         "id"6378,  
  4.         "title""test",  
  5.         "img""http://image.jxvdy.com/2014/0929/5428d91c9e6dc8f78fd99_0.png",  
  6.         "score"0,  
  7.         "description""test",  
  8.         "time"1411963174  
  9.     },  
  10.     {  
  11.         "id"6142,  
  12.         "title""微电影多角度拍摄技巧(三)",  
  13.         "img""http://image.jxvdy.com/old/201409/24/11-54-15-17-1531.jpg",  
  14.         "score"0,  
  15.         "description""",  
  16.         "time"1411530850  
  17.     },  
  18.     {  
  19.         "id"6141,  
  20.         "title""微电影多角度拍摄技巧(一)",  
  21.         "img""http://image.jxvdy.com/old/201409/24/11-54-04-89-1531.jpg",  
  22.         "score"0,  
  23.         "description""",  
  24.         "time"1411530835  
  25.     },  
  26.     {  
  27.         "id"6140,  
  28.         "title""微电影多角度拍摄技巧(二)",  
  29.         "img""http://image.jxvdy.com/old/201409/24/11-49-54-18-1531.jpg",  
  30.         "score"0,  
  31.         "description""",  
  32.         "time"1411530552  
  33.     },  
  34.     {  
  35.         "id"4355,  
  36.         "title""施比受,更有福",  
  37.         "img""http://image.jxvdy.com/old/201409/24/11-46-06-65-3.jpg",  
  38.         "score"0,  
  39.         "description""一位老人用自己的一半时间去帮助他人,赠予帮助,收获快乐",  
  40.         "time"1411530082  
  41.     },  
  42.     {  
  43.         "id"4354,  
  44.         "title""父子时光之旅",  
  45.         "img""http://image.jxvdy.com/old/201409/24/11-35-13-81-3.jpg",  
  46.         "score"0,  
  47.         "description""当父亲老去,忙于生活的男人没有时间照顾体弱的父亲,于是,带上父亲上路吧,带他重走当年他走过无数遍的那段旅程",  
  48.         "time"1411529699  
  49.     }  
  50. ]  
[
    {
        "id": 6378,
        "title": "test",
        "img": "http://image.jxvdy.com/2014/0929/5428d91c9e6dc8f78fd99_0.png",
        "score": 0,
        "description": "test",
        "time": 1411963174
    },
    {
        "id": 6142,
        "title": "微电影多角度拍摄技巧(三)",
        "img": "http://image.jxvdy.com/old/201409/24/11-54-15-17-1531.jpg",
        "score": 0,
        "description": "",
        "time": 1411530850
    },
    {
        "id": 6141,
        "title": "微电影多角度拍摄技巧(一)",
        "img": "http://image.jxvdy.com/old/201409/24/11-54-04-89-1531.jpg",
        "score": 0,
        "description": "",
        "time": 1411530835
    },
    {
        "id": 6140,
        "title": "微电影多角度拍摄技巧(二)",
        "img": "http://image.jxvdy.com/old/201409/24/11-49-54-18-1531.jpg",
        "score": 0,
        "description": "",
        "time": 1411530552
    },
    {
        "id": 4355,
        "title": "施比受,更有福",
        "img": "http://image.jxvdy.com/old/201409/24/11-46-06-65-3.jpg",
        "score": 0,
        "description": "一位老人用自己的一半时间去帮助他人,赠予帮助,收获快乐",
        "time": 1411530082
    },
    {
        "id": 4354,
        "title": "父子时光之旅",
        "img": "http://image.jxvdy.com/old/201409/24/11-35-13-81-3.jpg",
        "score": 0,
        "description": "当父亲老去,忙于生活的男人没有时间照顾体弱的父亲,于是,带上父亲上路吧,带他重走当年他走过无数遍的那段旅程",
        "time": 1411529699
    }
]

对于这一种json数据,使用fastjson进行解析的时候,调用方法之前应该先写出其对应的bean.java(我想你已经做过了);上面的json数据对应的bean是这样的,

  1. public class NewMoviesBean {  
  2.   
  3.     private int id;  
  4.     private String title;  
  5.     private String img;  
  6.     private String score;  
  7.     private String description;  
  8.     private int time;  
  9.     public int getId() {  
  10.         return id;  
  11.     }  
  12.     public void setId(int id) {  
  13.         this.id = id;  
  14.     }  
  15.     public String getTitle() {  
  16.         return title;  
  17.     }  
  18.     public void setTitle(String title) {  
  19.         this.title = title;  
  20.     }  
  21.     public String getImg() {  
  22.         return img;  
  23.     }  
  24.     public void setImg(String img) {  
  25.         this.img = img;  
  26.     }  
  27.     public String getScore() {  
  28.         return score;  
  29.     }  
  30.     public void setScore(String score) {  
  31.         this.score = score;  
  32.     }  
  33.     public String getDescription() {  
  34.         return description;  
  35.     }  
  36.     public void setDescription(String description) {  
  37.         this.description = description;  
  38.     }  
  39.     public int getTime() {  
  40.         return time;  
  41.     }  
  42.     public void setTime(int time) {  
  43.         this.time = time;  
  44.     }  
  45.     public NewMoviesBean(int id, String title, String img, String score,  
  46.             String description, int time) {  
  47.         super();  
  48.         this.id = id;  
  49.         this.title = title;  
  50.         this.img = img;  
  51.         this.score = score;  
  52.         this.description = description;  
  53.         this.time = time;  
  54.     }  
  55.     public NewMoviesBean() {  
  56.         super();  
  57.     }  
  58.     @Override  
  59.     public String toString() {  
  60.         return "NewMoviesBean [id=" + id + ", title=" + title + ", img=" + img  
  61.                 + ", score=" + score + ", description=" + description  
  62.                 + ", time=" + time + "]";  
  63.     }  
  64.       
  65.       
  66. }  
public class NewMoviesBean {

	private int id;
	private String title;
	private String img;
	private String score;
	private String description;
	private int time;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public String getImg() {
		return img;
	}
	public void setImg(String img) {
		this.img = img;
	}
	public String getScore() {
		return score;
	}
	public void setScore(String score) {
		this.score = score;
	}
	public String getDescription() {
		return description;
	}
	public void setDescription(String description) {
		this.description = description;
	}
	public int getTime() {
		return time;
	}
	public void setTime(int time) {
		this.time = time;
	}
	public NewMoviesBean(int id, String title, String img, String score,
			String description, int time) {
		super();
		this.id = id;
		this.title = title;
		this.img = img;
		this.score = score;
		this.description = description;
		this.time = time;
	}
	public NewMoviesBean() {
		super();
	}
	@Override
	public String toString() {
		return "NewMoviesBean [id=" + id + ", title=" + title + ", img=" + img
				+ ", score=" + score + ", description=" + description
				+ ", time=" + time + "]";
	}
	
	
}


那么对应的解析方法是这样的:

  1. JSON.parseArray(json, NewMoviesBean.class);  
JSON.parseArray(json, NewMoviesBean.class);
  1. 为甚么回事这种解析方式呢?因为,分析整个json数据的格式我们能发现,最外层是中括号"[ ]",内侧是大括号"{ }";中括号说明整个json数据为一个数组类型,其中的大括号说明是数组中的元素;说明整个就是一个JSONArray,JSONArray中元素又是一个个的JSONObject。  
为甚么回事这种解析方式呢?因为,分析整个json数据的格式我们能发现,最外层是中括号"[ ]",内侧是大括号"{ }";中括号说明整个json数据为一个数组类型,其中的大括号说明是数组中的元素;说明整个就是一个JSONArray,JSONArray中元素又是一个个的JSONObject。

另一种的解析方式:json数据是这样的,

  1. {"type": [  
  2.         "恐怖",  
  3.         "剧情"  
  4.     ]},  
{"type": [
        "恐怖",
        "剧情"
    ]},

分析这种形式,大括号里面是小括号。也即是数组整体是通过键值对的形式呈现的。那么最外层就是一个JSONObject,KEY对应的就是JSONArray。应该这样:

  1. JSONArray jsonArrayType = JSONObject.getJSONArray("type");  
  2.             String[] type = new String[jsonArrayType.size()];  
  3.             for (int j = 0; j < jsonArrayType.size(); j++) {  
  4.                 type[j] = (String)jsonArrayType.get(j);  
  5.             }  
JSONArray jsonArrayType = JSONObject.getJSONArray("type");
			String[] type = new String[jsonArrayType.size()];
			for (int j = 0; j < jsonArrayType.size(); j++) {
				type[j] = (String)jsonArrayType.get(j);
			}

这样就能够解析出想要的数据。

与上面类似的另一种解析:json数据是这样的:

  1. {  
  2.         "playurl": {  
  3.             "360P""http://v.jxvdy.com/sendfile/V7bzjsH5sIZlBzVG7t7qbL1u-y1_k6E0DCtzyZ8iv-pRF3GmewWOj-HQ_grNppGnnx_rRHb-bztNWAvzGQ",  
  4.             "480P""http://v.jxvdy.com/sendfile/V7bzjsH5sIZlBzVG7t7qbL1u-y1_k6E0DCtzyZ8iv-pRF3GmewWOj-HQ_grNppGnnx_rRHb-bztNWAvzGT",  
  5.             "720P""http://v.jxvdy.com/sendfile/V7bzjsH5sIZlBzVG7t7qbL1u-y1_k6E0DCtzyZ8iv-pRF3GmewWOj-HQ_grNppGnnx_rRHb-bztNWAvzGZ"  
  6.         }  
  7.     }  
{
        "playurl": {
            "360P": "http://v.jxvdy.com/sendfile/V7bzjsH5sIZlBzVG7t7qbL1u-y1_k6E0DCtzyZ8iv-pRF3GmewWOj-HQ_grNppGnnx_rRHb-bztNWAvzGQ",
            "480P": "http://v.jxvdy.com/sendfile/V7bzjsH5sIZlBzVG7t7qbL1u-y1_k6E0DCtzyZ8iv-pRF3GmewWOj-HQ_grNppGnnx_rRHb-bztNWAvzGT",
            "720P": "http://v.jxvdy.com/sendfile/V7bzjsH5sIZlBzVG7t7qbL1u-y1_k6E0DCtzyZ8iv-pRF3GmewWOj-HQ_grNppGnnx_rRHb-bztNWAvzGZ"
        }
    }


这种形式,外层大括号里面是一个键KEY对应了另一个大括号元素,那么其最外层是一个JSONObject;内层KEY对应的也是一个JSONObject。

当然也可以先创建开一个bean:

  1. public class MoviedefinitionBean {  
  2.   
  3.     private String normalP;  
  4.     private String hightP;  
  5.     private String superP;  
  6.     public String getNormalP() {  
  7.         return normalP;  
  8.     }  
  9.     public void setNormalP(String normalP) {  
  10.         this.normalP = normalP;  
  11.     }  
  12.     public String getHightP() {  
  13.         return hightP;  
  14.     }  
  15.     public void setHightP(String hightP) {  
  16.         this.hightP = hightP;  
  17.     }  
  18.     public String getSuperP() {  
  19.         return superP;  
  20.     }  
  21.     public void setSuperP(String superP) {  
  22.         this.superP = superP;  
  23.     }  
  24.     public MoviedefinitionBean(String normalP, String hightP, String superP) {  
  25.         super();  
  26.         this.normalP = normalP;  
  27.         this.hightP = hightP;  
  28.         this.superP = superP;  
  29.     }  
  30.     public MoviedefinitionBean() {  
  31.         super();  
  32.     }  
  33.     @Override  
  34.     public String toString() {  
  35.         return "MoviedefinitionBean [normalP=" + normalP + ", hightP=" + hightP  
  36.                 + ", superP=" + superP + "]";  
  37.     }  
  38.       
  39. }  
public class MoviedefinitionBean {

	private String normalP;
	private String hightP;
	private String superP;
	public String getNormalP() {
		return normalP;
	}
	public void setNormalP(String normalP) {
		this.normalP = normalP;
	}
	public String getHightP() {
		return hightP;
	}
	public void setHightP(String hightP) {
		this.hightP = hightP;
	}
	public String getSuperP() {
		return superP;
	}
	public void setSuperP(String superP) {
		this.superP = superP;
	}
	public MoviedefinitionBean(String normalP, String hightP, String superP) {
		super();
		this.normalP = normalP;
		this.hightP = hightP;
		this.superP = superP;
	}
	public MoviedefinitionBean() {
		super();
	}
	@Override
	public String toString() {
		return "MoviedefinitionBean [normalP=" + normalP + ", hightP=" + hightP
				+ ", superP=" + superP + "]";
	}
	
}


然后对此做出解析:

  1. JSONObject jsonObjectDefination = jsonObject.getJSONObject("playurl");  
  2.                 String normalP = jsonObjectDefination.getString("360P");  
  3.                 String hightP = jsonObjectDefination.getString("480P");  
  4.                 String superP = jsonObjectDefination.getString("720P");  
  5.                 playurl = new MoviedefinitionBean(normalP, hightP, superP);  
JSONObject jsonObjectDefination = jsonObject.getJSONObject("playurl");
				String normalP = jsonObjectDefination.getString("360P");
				String hightP = jsonObjectDefination.getString("480P");
				String superP = jsonObjectDefination.getString("720P");
				playurl = new MoviedefinitionBean(normalP, hightP, superP);

今天先写到这里|10-02-2014.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值