如何使用Gson逐个从JSON中读取变量?

huangapple 未分类评论45阅读模式
英文:

How to read variables one at a time from JSON using Gson?

问题

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

但是我不想将其保存为单个对象,我希望能够通过它们的名称获取单个变量的值,然后将这些值存储在单独的字段中,就像下面的示例中一样:

File file = "json.json";
Boolean javaVar1 = file.get("var1");
Boolean javaVar2 = file.get("var2");
英文:

I have a JSON file with contents {"var1" : true, "var2" : false} that I want to read using Gson.

But instead of saving it as a single object, I want to be able to get the values of single variables using their names to store them in seperate fields, like in the example below:

File file = "json.json";
Boolean javaVar1 = file.get("var1");
Boolean javaVar2 = file.get("var2");

答案1

得分: 0

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

你是指读取文件并提取值

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 {
    //给定
    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}";
    //当
    Map<String, Boolean> map = new Gson().fromJson(json, LinkedTreeMap.class);
    Boolean javaVar1 = map.get("var1");
    Boolean javaVar2 = map.get("var2");
    //那么
    assertEquals(true, javaVar1);
    assertEquals(false, javaVar2);
  }
}

或者正确地查找值

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 {
    //给定
    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("{}"); //--&gt; =  "{"var1" : true, "var2" : false}";
    //当
    Boolean javaVar1 = readJson(json, "var1");
    Boolean javaVar2 = readJson(json, "var2");
    //那么
    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;
  }
}
英文:

I've written some tests maybe it'll help you

You mean to read the files and pull the values

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 = &quot;G:\\praca\\app\\stackoverflow\\mijaGson\\src\\test\\java\\pl\\jac\\mija\\gson\\simple\\json.json&quot;;
    String json = Files.lines(Paths.get(path), StandardCharsets.UTF_8).findFirst().orElse(&quot;{}&quot;); //--&gt; =  &quot;{\&quot;var1\&quot; : true, \&quot;var2\&quot; : false}&quot;;
    //when
    Map&lt;String, Boolean&gt; map = new Gson().fromJson(json, LinkedTreeMap.class);
    Boolean javaVar1 = map.get(&quot;var1&quot;);
    Boolean javaVar2 = map.get(&quot;var2&quot;);
    //then
    assertEquals(true, javaVar1);
    assertEquals(false, javaVar2);
  }

}

Or faind correctly value

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 = &quot;G:\\praca\\app\\stackoverflow\\mijaGson\\src\\test\\java\\pl\\jac\\mija\\gson\\simple\\json.json&quot;;
    String json = Files.lines(Paths.get(path), StandardCharsets.UTF_8).findFirst().orElse(&quot;{}&quot;); //--&gt; =  &quot;{\&quot;var1\&quot; : true, \&quot;var2\&quot; : false}&quot;;
    //when
    Boolean javaVar1 = readJson(json, &quot;var1&quot;);
    Boolean javaVar2 = readJson(json, &quot;var2&quot;);
    //then
    assertEquals(true, javaVar1);
    assertEquals(false, javaVar2);
  }

  private Boolean readJson(String json, String keyValue) throws IOException {
    JsonReader in = new JsonReader(new StringReader(json));
    TypeAdapter&lt;Object&gt; 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;
  }

  ;
}

huangapple
  • 本文由 发表于 2020年4月7日 02:10:04
  • 转载请务必保留本文链接:https://java.coder-hub.com/61066177.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定