python json和java json的使用

python json和java json的使用

1、python json

import json
data={  
     "id" : 123,  
     "name" : "Henry Smith",  
     "age" : 28,  
     "salary" : 2000,  
     "designation" : "Programmer",  
     "address" : {  
        "street" : "Park Avn.",  
        "city" : "Westchester",  
        "zipcode" : 10583  
     },  
    "phoneNumbers" : [ 654321, 222333 ],  
    "personalInformation" : {  
        "gender" : "Male",  
        "maritialstatus" : "Married"  
    }  
} 
print type(data)                    # <type 'dict'>
print type(data['phoneNumbers'])    # <type 'list'>
print type(data['address'])         # <type 'dict'>
print data.keys()
d1 = json.dumps(data)               # json.dumps()是将字典转化为json字符串
print type(d1)                      # <type 'str'>

print d1                            #{
                                    #    "salary": 2000, 
                                    #     "phoneNumbers": [654321, 222333], 
                                    #     "name": "Henry Smith", 
                                    #     "designation": "Programmer", 
                                    #     "address": {
                                    #            "city": "Westchester", 
                                    #            "street": "Park Avn.", 
                                    #            "zipcode": 10583
                                    #      }, 
                                    #      "personalInformation": {
                                    #            "gender": "Male", 
                                    #             "maritialstatus": "Married"
                                    #      }, 
                                    #     "age": 28, 
                                    #     "id": 123
                                    #}
d2 = json.loads(d1)                 # json.loads()是将json字符串转化为字典
print type(d2)                      # <type 'dict'>
print d2                            # {u'salary': 2000, u'phoneNumbers': [654321, 222333], u'name': u'Henry Smith', 
                                    # u'designation': u'Programmer', u'age': 28, u'address': {u'city': u'Westchester', 
                                    # u'street': u'Park Avn.', u'zipcode': 10583}, u'personalInformation': 
                                    # {u'gender': u'Male', u'maritialstatus': u'Married'}, u'id': 123}
2、java json

1)java json的相关包:json-lib-2.4-jdk15,其依赖包有:ezmorph-1.0.6、commons-logging-1.1.1、commons-lang-2.5、commons-collections-3.2.1和

commons-beanutils-1.8.0

2)基本用法

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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

public class Test {

	public static void main(String[] args) {
		String data = "{"
				+"'id': 123,"
				+"'name' : 'Henry Smith',"
				+"'age' : 28,"
				+"'salary': 2000,"
				+"'designation' : 'Programmer',"
				+"'address' : {"
					+"'street' : 'Park Avn.',"
					+"'city' : 'Westchester',"
					+"'zipcode' : 10583"
				+"},"
				+"'phoneNumbers' : [ 654321, 222333 ],"
				+"'personalInformation' : {"  
		        +"'gender' : 'Male', " 
		        +"'maritialstatus' : 'Married'"  
		    +"}" 
		+"}";
		JSONObject json = JSONObject.fromObject(data);
		System.out.println(json.get("id"));
		System.out.println(json.getJSONArray("phoneNumbers"));
		System.out.println(json.getJSONArray("phoneNumbers").get(0));
		System.out.println(json.get("address"));
		System.out.println(json.getJSONObject("address").get("city"));
		// 数组转化为 jsonArray
		int[] phoneNumbers = {654321, 222333};
		JSONArray jsonArray = JSONArray.fromObject(phoneNumbers);
		System.out.println(jsonArray.toString());
		// List转化为 jsonArray
		List<Integer> phoneNumbers2 = new ArrayList<Integer>();
		phoneNumbers2.add(654321);phoneNumbers2.add(222333);
		JSONArray jsonArray2 = JSONArray.fromObject(phoneNumbers2);
		System.out.println(jsonArray2.toString());
		// Map转化为 jsonObject
		Map<String, Object> map = new HashMap<String,Object>();
		map.put("id", 123);
		map.put("age", 28);
		map.put("phoneNumbers", phoneNumbers);
		Map<String, Object> map2 = new HashMap<String,Object>();
		map2.put("street", "Park Avn.");
		map2.put("city", "Westchester");
		map2.put("zipcode", "10583");
		map.put("address", map2);
		JSONObject jsonMap = JSONObject.fromObject(map);
		System.out.println(jsonMap.toString());	
	}
	
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值