四、 nextValue方法比较关键,它流转解析的大部分工作!在nextValue中有一个readLiteral方法,针对一些类型做处理,得到解析之后的结果:
private Object readLiteral() throws JSONException { String literal = nextToInternal('{}[]/\:,=;# '); if (literal.length() == 0) { throw syntaxError('Expected literal value'); } else if ('null'.equalsIgnoreCase(literal)) { return JSONObject.NULL; } else if ('true'.equalsIgnoreCase(literal)) { return Boolean.TRUE; } else if ('false'.equalsIgnoreCase(literal)) { return Boolean.FALSE; } /* try to parse as an integral type... */ if (literal.indexOf('.') == -1) { int base = 10; String number = literal; if (number.startsWith('0x') || number.startsWith('0X')) { number = number.substring(2); base = 16; } else if (number.startsWith('0') && number.length() > 1) { number = number.substring(1); base = 8; } try { long longValue = Long.parseLong(number, base); if (longValue <= Integer.MAX_VALUE && longValue >= Integer.MIN_VALUE) { return (int) longValue; } else { return longValue; } } catch (NumberFormatException e) { /* * This only happens for integral numbers greater than * Long.MAX_VALUE, numbers in exponential form (5e-10) and * unquoted strings. Fall through to try floating point. */ } } /* ...next try to parse as a floating point... */ try { return Double.valueOf(literal); } catch (NumberFormatException ignored) { } /* ... finally give up. We have an unquoted string */ return new String(literal); // a new string avoids leaking memory }五、至于JSONArray的解析与JsonObject的解析过程是一样的,它里面维护的是一个List:
private final List<Object> values; public JSONArray(JSONTokener readFrom) throws JSONException { Object object = readFrom.nextValue(); if (object instanceof JSONArray) { values = ((JSONArray) object).values; } else { throw JSON.typeMismatch(object, 'JSONArray'); } } Gson的使用一、我们在测试当中先加入一个Person类,方便测试:
package com.tomhu.test; public class Person { private String name; private int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }二、 gson把对象转换成JSON格式
Gson gson = new Gson(); Person person = new Person(); person.setName('linux'); person.setAge(23); String str = gson.toJson(person); System.out.println(str); 打印结果: {'name':'linux','age':23}三、 gson把json格式解析成对象
String jsonData = '{'name':'刘力','age':19}'; Person person = gson.fromJson(jsonData, Person.class); System.out.println(person.getName() + ', ' + person.getAge()); 打印结果: 刘力, 19四、 gson把List对象解析成Json格式:
Gson gson = new Gson(); List<Person> persons = new ArrayList<Person>(); for (int i = 0; i < 2; i++) { Person p = new Person(); p.setName('name' + i); p.setAge(i * 5); persons.add(p); } String str = gson.toJson(persons); System.out.println(str); 打印结果: [{'name':'name0','age':0},{'name':'name1','age':5}]五、 gson把Json格式解析成List对象:
Gson gson = new Gson(); String str = '[{'name':'linux','age':10},{'name':'huhx','age':22}]'; List<Person> ps = gson.fromJson(str, new TypeToken<List<Person>>(){}.getType()); for (int i = 0; i < ps.size(); i++) { Person person = ps.get(i); System.out.print('name: ' + person.getName() + ' age: ' + person.getAge()); } 打印结果:name: linux age: 10 name: huhx age: 22延伸阅读:
返回到首页 返回到编程大巴