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());
}
}