出于某种原因,我在GSON
解析来自JSON_STRING
. 当我尝试解析时,我返回 null。你能帮我写下下面的代码,让我知道我哪里出错了吗?我的目标是最终从 url 解析 JSON 提要,但我想对字符串进行硬编码以用于测试目的。
import com.google.gson.Gson;
public class ReadJson {
public static void main(String... args) throws Exception {
String JSON_STRING =
"{\"data\":[{\"NAME\":\"Brandy\",\"LOCATION\":\"Redding, CA\"},
{\"NAME\":\"Jacob\",\"LOCATION\":\"Redding, CA\"},
{\"NAME\":\"Tatiana\",\"LOCATION\":\"Wonderland\"},
{\"NAME\":\"Tedo\",\"LOCATION\":\"Cottonwood\"}]}";
DataJSON data2 = new Gson().fromJson(JSON_STRING, DataJSON.class);
System.out.println(data2);
}
}
class DataJSON {
public String NAME;
public String LOCATION;
public String getName() { return NAME; }
public String getLocation() { return LOCATION;}
public void setName(String NAME) { this.NAME = NAME; }
public void setLoction(String LOCATION) { this.LOCATION = LOCATION; }
public String toString() {
return String.format("NAME:%s,LOCATION:%s", NAME, LOCATION);
}
}