String content = "{'username': 'linux', 'password': '123456'}";
JSONObject jsonObject = new JSONObject(content);
String username = jsonObject.getString("username");
String password = jsonObject.getString("password");
String jsonContent = "[{'user': '刘力', 'age': 21, 'femal': true}, "
+ "{'user': 'chen', 'age': 20, 'femal': false}]";
JSONArray jsonArray = new JSONArray(jsonContent);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject object = jsonArray.getJSONObject(i);
System.out.print(object.getString("user") + " ");
System.out.print(object.getInt("age") + " ");
System.out.print(object.getBoolean("femal") + " ");
System.out.println();
}
String jsonString = "[{'user': 'tomhu', 'age': 21, " + "'info': {'adress': 'hubai', 'sex': 'femal'}}, "
+ "{'user': 'chen', 'age': 20, " + "'info': {'adress': 'hunan', 'sex': 'male'}}]";
JSONArray jsonArrays = new JSONArray(jsonString);
for (int i = 0; i < jsonArrays.length(); i++) {
JSONObject objects = jsonArrays.getJSONObject(i);
System.out.print(objects.getString("user") + " ");
System.out.print(objects.getInt("age") + " ");
System.out.print(objects.getJSONObject("info").getString("adress") + " ");
System.out.print(objects.getJSONObject("info").getString("sex") + " ");
System.out.println();
}
Person person = new Person();
person.setUsername("linux" );
person.setPassword("123456" );
JSONArray jsonArray = new JSONArray();
jsonArray.put(0, person );
jsonArray.put(1, "I love you" );
// String username = jsonArray.getJSONObject(0).getString("username"); 错误的写法
Person user = (Person) jsonArray.get(0);
System.out.println("username: " + user.getUsername());
private final LinkedHashMap<String, Object> nameValuePairs;
public JSONObject() {
nameValuePairs = new LinkedHashMap<String, Object>();
}
public JSONObject put(String name, boolean value) throws JSONException {
nameValuePairs.put(checkName(name), value);
return this;
}
public String getString(String name) throws JSONException {
Object object = get(name); // get()方法就是执行Object result = nameValuePairs.get(name);
String result = JSON.toString(object);
if (result == null) {
throw JSON.typeMismatch(name, object, "String");
}
return result;
}
public JSONObject(String json) throws JSONException {
this(new JSONTokener(json));
}
public Object nextValue() throws JSONException {
int c = nextCleanInternal();
switch (c) {
case -1:
throw syntaxError("End of input");
case '{':
return readObject();
case '[':
return readArray();
case '\'':
case '"':
return nextString((char) c);
default:
pos--;
return readLiteral();
}
}
int first = nextCleanInternal(); // 得到解析的字符
if (first == '}') {
return result;
} else if (first != -1) {
pos--;
}
.......
while (true) {
Object name = nextValue(); // 解析得到键
int separator = nextCleanInternal();
if (separator != ':' && separator != '=') {
throw syntaxError("Expected ':' after " + name);
}
if (pos < in.length() && in.charAt(pos) == '>') {
pos++;
}
result.put((String) name, nextValue()); // 将解析得到的键值对,存放在map当中
switch (nextCleanInternal()) {
case '}':
return result;
case ';':
case ',':
continue;
default:
throw syntaxError("Unterminated object");
}
}
private Object readLiteral() throws JSONException {
String literal = nextToInternal("{}[]/\\:,=;# \t\f");
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
}
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");
}
}
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 gson = new Gson();
Person person = new Person();
person.setName("linux");
person.setAge(23);
String str = gson.toJson(person);
System.out.println(str);
String jsonData = "{'name':'刘力','age':19}";
Person person = gson.fromJson(jsonData, Person.class);
System.out.println(person.getName() + ", " + person.getAge());
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);
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());
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有