android之JSON数据的构建

JSON(JavaScript Object Notation)

 

——JSON是一种轻量级的数据交换语言,以文字为基础,且易于让人阅读。尽管

JSON是在JavaScript的一个子集,JSON是独立于语言的文本格式,并且采用了类似于C语言家族的一些习惯。Android2.3之后可以在Android中直接解析JSON

 

——作用:在服务器与客户端之间进行数据传递

        XML比较: XML更轻便,效率更高

         XML可读性、描述性更差

 

——JSON建构有两种结构:

        ——“名称/对的集合(A collection of name/value pairs).不同的语言中,它被理解为对象(object),记录(record),结构(struct),字典(dictionary),哈希表(hash table),有键列表(keyed list),或者关联数组 associative array)。

        ——值的有序列表(An ordered list of values。在大部分语言中,它被理解为数组(array)。


——格式

        对象:{key1:value1,key2:value2,key3:value3...}

        数组:[value1,value2...]

        注意:value可以是字符串、数字、boolean、数组、对象

 

例如:

{                                                                    

姓名":"张三",

年龄":18,

性别":true,

电话":[11111111111,22222222222],

"成绩":{"数学":120,

"语文":100,

"英语":110,

"综合":{"物理":80,"化学":60,"生物":70}}

}


——构建JSON:可以使用下面两种方式进行构建

        ——JSONObject

        创建JSONObject对象后调用put方法追加数据

 

        ——JSONStringer

例如:

  

   JSONStringer jsonText=new JSONStringer();  
    // 首先是{,对象开始。object和endObject必须配对使用  
    jsonText.object();  
    jsonText.key("phone");  
    //键phone的值是数组。array和endArray必须配对使用  
    jsonText.array();  
    jsonText.value("12345678").value("87654321");  
    jsonText.endArray();  
      
    jsonText.key("name");  
    jsonText.value("yuanzhifei89");  
    jsonText.key("age");  
    jsonText.value(100);  
      
    jsonText.key("address");  
    // 键address的值是对象  
    jsonText.object();  
    jsonText.key("country");  
    jsonText.value("china");  
    jsonText.key("province");  
    jsonText.value("jiangsu");  
    jsonText.endObject();  
      
    jsonText.key("married");  
    jsonText.value(false);    
    // },对象结束  
    jsonText.endObject();
    String result=jsonText.toString();

写个例子啊:

1.通过JSonObject来构建

	/*
	 * 通过JSonObject来构建
	 */
	
	public void btn1(View view){
		JSONObject jsonObject=new JSONObject();
		try {
			jsonObject.put("name", "张三");
			jsonObject.put("age", 25);
			JSONArray phoneArray=new JSONArray();
			phoneArray.put("1454325");
			phoneArray.put("1366757");
			jsonObject.put("phone", phoneArray);
			JSONObject scoreObject=new JSONObject();
			scoreObject.put("语文", 100);
			JSONObject lizongObject=new JSONObject();
			lizongObject.put("化学", 60);
			lizongObject.put("物理", 57);
			scoreObject.put("理综", lizongObject);
			jsonObject.put("score", scoreObject);
			tv.setText(jsonObject.toString());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
2.通过JSONStringer去构建JSon:

	/*
	 * 通过JSONStringer去构建JSon
	 */
    public void btn2(View view){
		JSONStringer jsonStringer=new JSONStringer();
		try {
			jsonStringer.object();
			jsonStringer.key("name");
			jsonStringer.value("张三");
			jsonStringer.key("age");
			jsonStringer.value(24);
			jsonStringer.key("phone");
			jsonStringer.array();
			jsonStringer.value("123");
			jsonStringer.value("326743264");
			jsonStringer.endArray();
			jsonStringer.key("score");
			jsonStringer.object();
			jsonStringer.key("语文");
			jsonStringer.value(100);
			jsonStringer.key("理综");
			jsonStringer.object();
			jsonStringer.key("物理");
			jsonStringer.value(72);
			jsonStringer.key("化学");
			jsonStringer.value(67);
			jsonStringer.endObject();
			jsonStringer.endObject();
			jsonStringer.endObject();
			tv.setText(jsonStringer.toString());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

大家觉得哪种方式更简单呢~觉得哪种简单就用哪种吧~


效果图:


源码:下载














你可能感兴趣的:(json,数据,JSON数据构建)