标签:tor hashmap ++ numbers 3.3 ash ber http add
不同程序间的通信载体,使用json是比较方便的一种格式,当然,这种格式的好处晚上随便找找就能看到,如果说到坏处,那么可能就是全都是字符串传输,效率没那么高,但是对于一般开发的程序,尤其是互联网的程序而言,绰绰有余。gson库创建模式一种是new gson,默认方式。另外还有一种个性化配置的方式gsonBuilder。
1.json字符串转换为常见类型:int double String boolean
public static void testfromJson()
{
Gson gson = new Gson();
int number1 = gson.fromJson("999", int.class);
double number2 = gson.fromJson("99.99", double.class);
boolean number3 = gson.fromJson("true", boolean.class);
String str = gson.fromJson("helloworld", String.class);
numberSpecial number = gson.fromJson("{\"i\":1,\"j\":2.2,\"Address\":\"test\",\"flag\":true}", numberSpecial.class);
}
2.POJO类转换为json
public static void testObjectToJson()
{
Gson gson = new Gson();
numberSpecial number = new numberSpecial(1,2.2,"test",true);
String result = gson.toJson(number);
System.out.println(result);
numberSpecial number2 = gson.fromJson(result, numberSpecial.class);
System.out.println(number2.i+" "+number2.j+" "+number2.str+" "+number.flag);
}
3.json格式的字符串,转换为数组
public static void testArrayFromJson()
{
Gson gson = new Gson();
String jsonArray = "[\"USD\",\"CNY\",\"GBP\"]";
String [] strings = gson.fromJson(jsonArray, String[].class);
for(int i=0; i< strings.length; i++)
{
System.out.println(strings[i]);
}
}
4.将list和map集合对象,转换为json格式的字符串。
public static void toListJSON()
{
List<numberSpecial> list = new ArrayList<>();
Map<String,numberSpecial> map = new HashMap<>();
numberSpecial number = new numberSpecial(3,3.3,"List",true);
list.add(number);
map.put("test1", number);
numberSpecial number2 = new numberSpecial(3,3.3,"List",true);
number2.i=4;
list.add(number2);
map.put("test2", number2);
Gson gson = new Gson();
System.out.println("List json" + gson.toJson(list));
System.out.println("map json" + gson.toJson(map));
}
5.将json格式的字符串,转换为list和map集合对象。
public static void fromListJSON()
{
String jsonStr = "[{\"i\":3,\"j\":3.3,\"Address\":\"List\",\"flag\":true},{\"i\":4,\"j\":3.3,\"Address\":\"List\",\"flag\":true}]";
Gson gson = new Gson();
List<numberSpecial> list = gson.fromJson(jsonStr, new TypeToken<ArrayList<numberSpecial>>(){}.getType());
for(int i=0; i< list.size(); i++)
{
numberSpecial number2 = list.get(i);
System.out.println(number2.i+" "+number2.j+" "+number2.str+" "+number2.flag);
}
}
标签:tor hashmap ++ numbers 3.3 ash ber http add
原文地址:http://blog.51cto.com/ggwhsd/2342456