Json   发布时间:2022-04-22  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了JSONObject 保存和传送 数组数据 ,如int[] ,double[] ,String[]...大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

上图为转换后的显示效果。下面介绍如何转换

public class MainActivity extends Activity {

	private TextView show;

	@Override
	protected void onCreate(Bundle savedInstanceStatE) {
		super.onCreate(savedInstanceStatE);
		setContentView(R.layout.activity_main);
		show = (TextView) findViewById(R.id.show);
		jsonSe@R_419_1391@();

	}
	
	/**
	 * 用json发送数据 如int [],double[],String [] ....
	 * 这里是封装并发送 ,方法略显笨拙
	 */
	public void jsonSe@R_419_1391@(){
		int [] dou={1,2,3,4,5,6,7,8,9,0};
		JSONObject clientKey = new JSONObject(); 
		JSONArray jsonArray = new JSONArray(); //保存数组数据的JSONArray对象
		for(int i = 0 ; i < dou.length ;i++){  //依次将数组元素添加进JSONArray对象中
			jsonArray.put(dou[i]);
//          两种添加方式一样			
//			try {
//				jsonArray.put(i,dou[i]);
//			} catch (JSONException E) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}
      		}
		
		try {
			clientKey.put("intArray",jsonArray);//jsonArray相当于int数组
	        show.setText(clientKey.toString());   //输出测试
	      
		} catch (JSONException E) {
			e.printStackTrace();
		}
	}
}
下面介绍一些JSONObject 和JSONArray的一些基础用法

/**
	 * 一组数据转换成json
	 * */
		public  void String2json(){
			   String str="{\"name\":\"Iyangc\",\"age\":21,\"sex\":'男',\"address\":{\"province\":\"四川\",\"city\":\"成都\"}}";
			   try {
				JSONObject jsonObject = new JSONObject(str);
				String name = jsonObject.getString("name");
				JSONObject address = jsonObject.getJSONObject("address");
				String province = address.getString("province");
				int age = jsonObject.geTint("age"); 
				 show.setText("name: "+name+"\nprovince: "+province+"\nage: "+agE);
			   } catch (JSONException E) {
				e.printStackTrace();
			} 
		}
		/**
		 * 数组数据转换json
		 * */
		public  void json2String(){
			 String str= "[{\"name\":\"张一\",\"age\":22,\"address\":" +
						"{\"province\":\"四川\",\"city\":\"成都\",\"details\":\"光明街16号\"}}," +
						"{\"name\":\"张二\",\"age\":23,\"details\":\"建设路5号\"}}," +
						"{\"name\":\"张三\",\"details\":\"中山南路8号\"}}]";
		   try {
			JSONArray jsonArray = new  JSONArray(str); //将String转换成JsonArray对象
			      int length=jsonArray.length();
			      //取出数据
			 for (int i = 0; i < length; i++) {
				 JSONObject jsonObject = jsonArray.getJSONObject(i);
				 String name = jsonObject.getString("name");
				 JSONObject jsonObject2 = jsonObject.getJSONObject("address");
				 String province = jsonObject2.getString("province");
				 System.out.println(name+"-----------"+province);
			}
		} catch (JSONException E) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		}
		
		/**
		 * 以键值对写入json
		 * */
		public static void jsonArray2String(){
			JSONArray jsonArray = new  JSONArray();
			JSONObject jsonObject1= new  JSONObject();
			JSONObject jsonObject2= new  JSONObject();
			JSONObject jsonObject3= new  JSONObject();
			try {
				jsonObject1 .put("name","Iyangc");
				jsonObject1 .put("age",21);
				JSONObject addressObject1 = new  JSONObject();
				addressObject1.put("province","四川");
				jsonObject1.put("address",addressObject1 );
				
				
				jsonObject2 .put("name","ILan");
				jsonObject2 .put("age",22);
				JSONObject addressObject2 = new  JSONObject();
				addressObject2.put("province","四川简阳");
				jsonObject2.put("address",addressObject2 );
				
				
				jsonObject3 .put("name","IBo");
				jsonObject3 .put("age",21);
				JSONObject addressObject3 = new  JSONObject();
				addressObject3.put("province","四川石桥");
				jsonObject3.put("address",addressObject3 );
				
				jsonArray.put(jsonObject1);
				jsonArray.put(jsonObject2);
				jsonArray.put(jsonObject3);
				
				//打印输出转换成json格式后的String字符串
				String String = jsonArray.toString();
				System.out.println(String );
				
			} catch (JSONException E) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
就单独用JSON的话 解析起来会显得很麻烦 所以一般要和Gson一起使用。 Gson可以直接将Json类型的字符串转换成类对象,并且对象的属性值与Json的属性值相对应。Gson的一个免费下载地址:http://download.csdn.net/detail/a771948524/6668573

下载后 将其@L_696_17@导入工程就可以使用了。下面是Gson的一些基本用法

public class GsonDemo extends Activity {
	Ure ure1;
	Ure ure2;
	Ure ure3;
	ArrayList<Ure> ures;
	@Override
	protected void onCreate(Bundle savedInstanceStatE) {
		super.onCreate(savedInstanceStatE);
		setContentView(R.layout.gson_demo);
		ures=new ArrayList<Ure>();
		ure1=new Ure("Iyangc",21);
		ure2=new Ure("Iyang",22);
		ure3=new Ure("Iyan",23);
		ures.add(ure1);
		ures.add(ure2);
		ures.add(ure3);
//		strToJson();
//		 listToJson();
//		 strFromJson();
		 listFromJson();
	}
	
	/**
	 * 通过Gson将bean转化成JSON数据
	 * */
	public void  strToJson(){
         Gson g=new Gson();
          //通过Gson.toJson将bean转成Json对象
         String json = g.toJson(ure1);
		   System.out.println(json );
	}
	/**
	 * 通过Gson.toJson将bean集合转成Json对象
	 * */
	public void  listToJson(){
		Gson g=new Gson();
		//通过Gson.toJson将bean集合转成Json对象
		String json = g.toJson(ures);
		System.out.println(json );
	}
	/**
	 * 将JSON格式的数据解析出来~~
	 * */
	public void  strFromJson(){
		Ure ure=new Ure();
		Gson g=new Gson();
		String str="{\"name\":\"ILan\",\"age\":22}";
//			JSONObject json=new JSONObject(str);
			ure=g.fromJson(str,Ure.class);
			System.out.println(ure.name+" ======= "+ure.agE);
	}
	/**
	 * 将JSON格式的数据(集合)解析出来~~
	 * */
	public void  listFromJson(){
		Gson g=new Gson();
		String str="[{\"name\":\"ILan1\",\"age\":221},{\"name\":\"ILan2\",\"age\":222}]";
//			JSONObject json=new JSONObject(str);
      	           	 Type type = new  TypeToken<List<Ure>>(){  }.getType();
      	           	 //将JSON集合数据解析出来
      	           List<Ure> l=g.fromJson(str,typE);
		       for (Ure ure : l) {
		    	   System.out.println(ure.name+" ======= "+ure.agE);
			}
	}
}
class Ure{
	public String name;
	public int age;  
	public Ure(String name,int agE) {
		this.name = name;
		this.age = age;
	}
	public Ure() {
	}
}

在JSONObject 传送数组时 可以直接在bean类中定义一个数组 ,只要属性名对应且类型对应 用Gson可直接转换出来,就和基本类型一样。
如果 Ure类中多一个 double[] 数组对象dou,而且json中有对应数据 那么用Gson将json转换成use对象后 直接 use.dou就能获取这个数组数据。

Demo下载地址: http://download.csdn.net/detail/yangbo437993234/7626045 不懂的可以下来看看 都是很基础的

大佬总结

以上是大佬教程为你收集整理的JSONObject 保存和传送 数组数据 ,如int[] ,double[] ,String[]...全部内容,希望文章能够帮你解决JSONObject 保存和传送 数组数据 ,如int[] ,double[] ,String[]...所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。