GSON的基本用法五
演示JsonParser的用法
import com.google.gson.Gson; import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonParser; /** * @author yongjar * @date 2020/4/28 */ public class GsonTester { public static void main(String args[]) { //json串 String json = "{ \"f1\":\"Hello\",\"f2\":{\"f3:\":\"World\"}}"; //解析为json元素 JsonElement jsonElement = JsonParser.parseString(json); //将json元素转为jsonJsonObject JsonObject asJsonObject = jsonElement.getAsJsonObject(); //这里的get传入的是json元素的key JsonElement jsonElement1 = asJsonObject.get("f1"); //打印出Hello。 System.out.println(jsonElement1); } }