Gson可以使用 JsonElement 类型,对 Json 中的某些字段保持原样,不进行解析。
javabean
public class Data {
@SerializedName("id")
public int id;
@SerializedName("name")
public String name;
//附加字段,客户端不需要关心,透传给服务端接口
@SerializedName("extras")
public JsonElement extras;
}
json
{
"id": 837,
"name": "input_text",
"extras": {
"language": "en-us",
"enable": false,
"text": "life is hard",
"voice": "ivy",
"format": "wav",
"sr": 24000
}
}
解析键值对json
{
"a":"111",
"aa":"111",
"aaa":"111",
"aaaa":"111",
"aaaaa":"111"
}
import org.json.JSONObject;
try {
JSONObject jsonObjectMap = new JSONObject(mapJson);
Iterator<String> keys = jsonObjectMap.keys();
while (keys.hasNext()){
String key = keys.next();
String value = jsonObjectMap.getString(key);
System.out.println(key+"="+value);
}
} catch (JSONException e) {
e.printStackTrace();
}