英文:
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("{}"); //--> = "{"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 = "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);
}
}
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 = "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;
}
;
}
专注分享java语言的经验与见解,让所有开发者获益!
评论