标签:文件中 信息 tor sim 注意 ToJson hide error 微软
Person person = gson.fromJson(jsonString, Person.class);
Person person = gson.fromJson(jsonString, new TypeToken<Person>() {}.getType());
Person person = gson.fromJson(jsonString, Person.class);
Person person = gson.fromJson(jsonString, new TypeToken<Person>() {}.getType());
String[] array = gson.fromJson(jsonString, String[].class);
String[] array = gson.fromJson(jsonString, new TypeToken<String[]>() {}.getType());
int[][] array = gson.fromJson(jsonString, int[][].class);
String[] array = gson.fromJson(jsonString, String[].class);
String[] array = gson.fromJson(jsonString, new TypeToken<String[]>() {}.getType());
int[][] array = gson.fromJson(jsonString, int[][].class);
List<Person> list = gson.fromJson(jsonString, new TypeToken<List<Person>>() {}.getType());//支持带泛型
Map<String, Integer> map = gson.fromJson(jsonString2, new TypeToken<Map<String, Integer>>() {}.getType());
List<Person> list = gson.fromJson(jsonString, new TypeToken<List<Person>>() {}.getType());//支持带泛型
Map<String, Integer> map = gson.fromJson(jsonString2, new TypeToken<Map<String, Integer>>() {}.getType());
@SerializedName(value = "message", alternate = {"tips", "msg","errorinfo","error"})
public String message;
value = "message", alternate = {"tips", "msg","errorinfo","error"}) (
public String message;
public class Test {
public static void main(String[] args) {
String content = "{\"code\":\"100\",\"data\":{\"errorinfo\":\"成功\"}}";
String content2 = "{\"code\":\"100\",\"data\":\"成功\"}";
System.out.println(new Gson().fromJson(content, Response.class).data);//【{"errorinfo":"成功"}】
System.out.println(new Gson().fromJson(content2, Response.class).data);//【"成功"】
}
static class Response {
public String code;
public JsonElement data;//用一个JsonObject去接收不详解析的内容
}
}
public class Test {
public static void main(String[] args) {
String content = "{\"code\":\"100\",\"data\":{\"errorinfo\":\"成功\"}}";
String content2 = "{\"code\":\"100\",\"data\":\"成功\"}";
System.out.println(new Gson().fromJson(content, Response.class).data);//【{"errorinfo":"成功"}】
System.out.println(new Gson().fromJson(content2, Response.class).data);//【"成功"】
}
static class Response {
public String code;
public JsonElement data;//用一个JsonObject去接收不详解析的内容
}
}
{"errorinfo":"成功"}
"成功"
{"errorinfo":"成功"}
"成功"
{errorinfo=成功}
成功
{errorinfo=成功}
成功
String jsonString = "{‘flag‘:true,‘data‘:{‘name‘:‘张三‘,‘age‘:18,‘address‘:‘广州‘}}";
JsonObject root = new JsonParser().parse(jsonString).getAsJsonObject();
boolean flag = root.getAsJsonPrimitive("flag").getAsBoolean();
String name = root.getAsJsonObject("data").get("name").getAsString();
System.out.println(flag + " " + name);//true 张三
String jsonString = "{‘flag‘:true,‘data‘:{‘name‘:‘张三‘,‘age‘:18,‘address‘:‘广州‘}}";
JsonObject root = new JsonParser().parse(jsonString).getAsJsonObject();
boolean flag = root.getAsJsonPrimitive("flag").getAsBoolean();
String name = root.getAsJsonObject("data").get("name").getAsString();
System.out.println(flag + " " + name);//true 张三
gson.toJson(user,System.out); //直接写到控制台。System.out是PrintStream的子类,PrintStream是Appendable接口的实现类
FileWriter fileWriter=new FileWriter("d://a.txt");//将toJson后的字符串拼接到文件中
gson.toJson(person, fileWriter);//FileWriter extends OutputStreamWriter extends Writer,Writer实现了Appendable接口
fileWriter.close();
StringBuilder builder=new StringBuilder();//将toJson后的字符串拼接到StringBuilder中
gson.toJson(person, builder);//StrngBuilder、StringBuffer都是Appendable接口的实现类
System.out.println(builder.toString());
gson.toJson(user,System.out); //直接写到控制台。System.out是PrintStream的子类,PrintStream是Appendable接口的实现类
FileWriter fileWriter=new FileWriter("d://a.txt");//将toJson后的字符串拼接到文件中
gson.toJson(person, fileWriter);//FileWriter extends OutputStreamWriter extends Writer,Writer实现了Appendable接口
fileWriter.close();
StringBuilder builder=new StringBuilder();//将toJson后的字符串拼接到StringBuilder中
gson.toJson(person, builder);//StrngBuilder、StringBuffer都是Appendable接口的实现类
System.out.println(builder.toString());
JsonWriter writer = new JsonWriter(new OutputStreamWriter(System.out));//可以先写到文件中,再从文件中读取
writer.beginObject().name("name").value("包青天").name("sport").beginArray().value("篮球").value("排球").endArray()//数据
.name("info").beginObject().name("age").value(28).endObject()//对象
.endObject();
writer.close(); //{"name":"包青天","sport":["篮球","排球"],"info":{"age":28}}
x
JsonWriter writer = new JsonWriter(new OutputStreamWriter(System.out));//可以先写到文件中,再从文件中读取
writer.beginObject().name("name").value("包青天").name("sport").beginArray().value("篮球").value("排球").endArray()//数据
.name("info").beginObject().name("age").value(28).endObject()//对象
.endObject();
writer.close(); //{"name":"包青天","sport":["篮球","排球"],"info":{"age":28}}
final StringBuilder builder = new StringBuilder();
Writer writer = new Writer() {
@Override
public void write(@NonNull char[] cbuf, int off, int len) throws IOException {
builder.append((new String(cbuf, off, len)));
}
@Override
public void flush() throws IOException {
System.out.println("【flush】");
}
@Override
public void close() throws IOException {
System.out.println("【close】");
System.out.println(builder.toString());
}
};
JsonWriter jsonWriter = new JsonWriter(writer);//可以先写到文件中,再从文件中读取
jsonWriter.setIndent(" ");//设置缩进格式,一般使用2-4个空格,或使用制表符\t
jsonWriter.beginObject().name("name").value("包青天").name("sport").beginArray().value("篮球").value("排球").endArray()//数据
.name("info").beginObject().name("age").value(28).endObject()//对象
.endObject();
jsonWriter.close(); //{"name":"包青天","sport":["篮球","排球"],"info":{"age":28}}
final StringBuilder builder = new StringBuilder();
Writer writer = new Writer() {
public void write( char[] cbuf, int off, int len) throws IOException {
builder.append((new String(cbuf, off, len)));
}
public void flush() throws IOException {
System.out.println("【flush】");
}
public void close() throws IOException {
System.out.println("【close】");
System.out.println(builder.toString());
}
};
JsonWriter jsonWriter = new JsonWriter(writer);//可以先写到文件中,再从文件中读取
jsonWriter.setIndent(" ");//设置缩进格式,一般使用2-4个空格,或使用制表符\t
jsonWriter.beginObject().name("name").value("包青天").name("sport").beginArray().value("篮球").value("排球").endArray()//数据
.name("info").beginObject().name("age").value(28).endObject()//对象
.endObject();
jsonWriter.close(); //{"name":"包青天","sport":["篮球","排球"],"info":{"age":28}}
{
"name": "包青天",
"sport": [
"篮球",
"排球"
],
"info": {
"age": 28
}
}
x
{
"name": "包青天",
"sport": [
"篮球",
"排球"
],
"info": {
"age": 28
}
}
标签:文件中 信息 tor sim 注意 ToJson hide error 微软
原文地址:https://www.cnblogs.com/baiqiantao/p/6cc6013b5279bcf0ec54909776a24a23.html