JSON学习:JsonArray遍历方法

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u012303775/article/details/81175474

1.Maven项目使用JSON需要的依赖包

<!-- 引入json依赖  -->
<dependency>
   <groupId>net.sf.json-lib</groupId>
   <artifactId>json-lib</artifactId>
   <version>2.4</version>
   <classifier>jdk15</classifier>
</dependency>
<dependency>
   <groupId>commons-beanutils</groupId>
   <artifactId>commons-beanutils</artifactId>
   <version>1.7.0</version>
</dependency>
<dependency>
   <groupId>commons-collections</groupId>
   <artifactId>commons-collections</artifactId>
   <version>3.1</version>
</dependency>
<dependency>
   <groupId>commons-lang</groupId>
   <artifactId>commons-lang</artifactId>
   <version>2.5</version>
</dependency>
<dependency>
   <groupId>net.sf.ezmorph</groupId>
   <artifactId>ezmorph</artifactId>
   <version>1.0.3</version>
</dependency>

2.遍历JSONArray

package cn.jm.action;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 *
 * @ClassName: Test
 * @Description: TODO
 * @Author Ming
 * @Date 2018年7月23日 下午8:41:28
 * @Vesion 1.0
 * 
 */
public class Test {

	public static void main(String[] args) {

		String str = "[{'status':'有效','name':'技限公司','code':'001'}," + 
			         "{'status':'无效','name':'有限公司','code':'005'}]";
		JSONArray json = JSONArray.fromObject(str);  
		if(json.size()>0){
		  for(int i=0;i<json.size();i++){
			  JSONObject job = json.getJSONObject(i);   // 遍历 jsonarray 数组,把每一个对象转成 json 对象
			  System.out.println(job.get("status")) ;   // 得到 每个对象中的属性值 
		  }
		}
	}
}

猜你喜欢

转载自blog.csdn.net/u012303775/article/details/81175474