json快速解析

写项目经常会用到 json 解析 为了方便自己 也方便大家 我写个demo 出来

依赖 (谷歌)

	<dependency>
			<groupId>com.google.code.gson</groupId>
			<artifactId>gson</artifactId>
			<version>2.8.0</version>
		</dependency>

代码


String str="你的json字符串"
   JsonObject json=(JsonObject) parse.parse(str);
   //获取外层的状态码
  int asInt = json.get("code").getAsInt();
 JsonObject dataobject = json.get("result").getAsJsonObject();
 //获取第二层的数组
 JsonArray  asJsonArray= dataobject.get("data").getAsJsonArray();
 

获取值得时候 不要为null 否则 json 解析会出错

猜你喜欢

转载自blog.csdn.net/qq_41684939/article/details/89554632