首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何使用Gson从JSON中一次读取一个变量?

如何使用Gson从JSON中一次读取一个变量?
EN

Stack Overflow用户
提问于 2020-04-07 02:10:04
回答 1查看 174关注 0票数 0

我有一个包含内容{"var1" : true, "var2" : false}的JSON文件,我想用Gson读取它。

但我不希望将其保存为单个对象,而是希望能够使用名称获取单个变量的值,以便将它们存储在单独的字段中,如下面的示例所示:

代码语言:javascript
运行
复制
File file = "json.json";
Boolean javaVar1 = file.get("var1");
Boolean javaVar2 = file.get("var2");
EN

回答 1

Stack Overflow用户

发布于 2020-04-07 17:49:43

我写了一些测试,也许会对你有帮助

您的意思是读取文件并提取值

代码语言:javascript
运行
复制
import com.google.gson.Gson;
import com.google.gson.internal.LinkedTreeMap;
import org.junit.Test;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Map;

import static org.junit.Assert.assertEquals;

public class GsonTest {
  @Test
  public void deserialize() throws IOException {
    //given
    String path = "G:\\praca\\app\\stackoverflow\\mijaGson\\src\\test\\java\\pl\\jac\\mija\\gson\\simple\\json.json";
    String json = Files.lines(Paths.get(path), StandardCharsets.UTF_8).findFirst().orElse("{}"); //--> =  "{\"var1\" : true, \"var2\" : false}";
    //when
    Map<String, Boolean> map = new Gson().fromJson(json, LinkedTreeMap.class);
    Boolean javaVar1 = map.get("var1");
    Boolean javaVar2 = map.get("var2");
    //then
    assertEquals(true, javaVar1);
    assertEquals(false, javaVar2);
  }

}

或模糊的正确值

代码语言:javascript
运行
复制
import com.google.gson.Gson;
import com.google.gson.TypeAdapter;
import com.google.gson.internal.bind.ObjectTypeAdapter;
import com.google.gson.reflect.TypeToken;
import com.google.gson.stream.JsonReader;
import org.junit.Test;

import java.io.IOException;
import java.io.StringReader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;

import static org.junit.Assert.assertEquals;

public class TestJsonReader {
  @Test
  public void deserialize() throws IOException {
    //given
    String path = "G:\\praca\\app\\stackoverflow\\mijaGson\\src\\test\\java\\pl\\jac\\mija\\gson\\simple\\json.json";
    String json = Files.lines(Paths.get(path), StandardCharsets.UTF_8).findFirst().orElse("{}"); //--> =  "{\"var1\" : true, \"var2\" : false}";
    //when
    Boolean javaVar1 = readJson(json, "var1");
    Boolean javaVar2 = readJson(json, "var2");
    //then
    assertEquals(true, javaVar1);
    assertEquals(false, javaVar2);
  }

  private Boolean readJson(String json, String keyValue) throws IOException {
    JsonReader in = new JsonReader(new StringReader(json));
    TypeAdapter<Object> objectTypeAdapter = ObjectTypeAdapter.FACTORY.create(new Gson(), TypeToken.get(Object.class));
    in.beginObject();
    while (in.hasNext()) {
      String key = in.nextName();
      if (key.equals(keyValue)) {
        return (Boolean) objectTypeAdapter.read(in);
      }
      in.skipValue();
    }
    in.endObject();
    return null;
  }

  ;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61066177

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档