使用GSON从Java中的JSON字符串获取值
我希望有人能告诉我哪里做错了 我正在使用sendgrid进行电子邮件跟踪,它发布了一个JSON,如下所示:使用GSON从Java中的JSON字符串获取值,java,json,string,gson,Java,Json,String,Gson,我希望有人能告诉我哪里做错了 我正在使用sendgrid进行电子邮件跟踪,它发布了一个JSON,如下所示: [ { "email": "john.doe@sendgrid.com", "timestamp": 1337966815, "event": "click", "url": "http://sendgrid.com" "userid": "1123", "template": "welcome" } ] StringB
[
{
"email": "john.doe@sendgrid.com",
"timestamp": 1337966815,
"event": "click",
"url": "http://sendgrid.com"
"userid": "1123",
"template": "welcome"
}
]
StringBuffer jb = new StringBuffer();
String line = null;
try {
BufferedReader reader = req.getReader();
while ((line = reader.readLine()) != null)
jb.append(line);
} catch (Exception e) { /*report an error*/ }
String jsonString = jb.toString();
Gson gson = new Gson();
JsonObject jsonObject = gson.fromJson(jsonString, JsonObject.class);
String timeStam = jsonObject.get(timestamp).toString();
现在,我想得到“timestamp”的值,例如1337966815。我尝试了以下方法:
[
{
"email": "john.doe@sendgrid.com",
"timestamp": 1337966815,
"event": "click",
"url": "http://sendgrid.com"
"userid": "1123",
"template": "welcome"
}
]
StringBuffer jb = new StringBuffer();
String line = null;
try {
BufferedReader reader = req.getReader();
while ((line = reader.readLine()) != null)
jb.append(line);
} catch (Exception e) { /*report an error*/ }
String jsonString = jb.toString();
Gson gson = new Gson();
JsonObject jsonObject = gson.fromJson(jsonString, JsonObject.class);
String timeStam = jsonObject.get(timestamp).toString();
jsonString字符串提供了以下内容,我认为格式正确:
[ { "email": "john.doe@sendgrid.com", "timestamp": 1337966815, "event": "click", "url": "http://sendgrid.com" "userid": "1123", "template": "welcome" }]
但是我在这行代码中得到了以下错误-JsonObject JsonObject=gson.fromJson(jsonString,JsonObject.class)
我做错了什么?是jsonString的格式混淆了JsonObject吗
任何帮助都将不胜感激
问候
Francois您在两个示例中显示的JSON无效。
“url”后缺少逗号:http://sendgrid.com“
忽略这一点,您显示的JSON是JSON对象的数组,而不是对象。这就是[]
所表示的(更正缺少的逗号):
如果您没有将此JSON映射到Java POJO,那么您可能希望使用Gson将字符串
解析为a(注意,您甚至可以使用它直接从流解析,但这是您现在如何拥有代码的前提)
现在您有了所谓的“解析树”。这个JsonElement
是根。要将其作为阵列访问,您需要执行以下操作:
JsonArray myArray = je.getAsJsonArray();
您只显示包含一个对象的数组,但假设它可以有多个对象。通过迭代数组,可以执行以下操作:
for (JsonElement e : myArray)
{
// Access the element as a JsonObject
JsonObject jo = e.getAsJsonObject();
// Get the `timestamp` element from the object
// since it's a number, we get it as a JsonPrimitive
JsonPrimitive tsPrimitive = jo.getAsJsonPrimitive("timestamp");
// get the primitive as a Java long
long timestamp = tsPrimitive.getAsLong();
System.out.println("Timestamp: " + timestamp);
}
认识到Gson主要用于对象关系映射,您希望将该JSON转换为Java对象。这实际上要简单得多:
因为您有这些对象的数组,所以需要使用TypeToken
和Type
来指示您的JSON是这些ResponseObject
对象的列表
:
Type myListType = new TypeToken<List<ResponseObject>>(){}.getType();
List<ResponseObject> myList = new Gson().fromJson(jsonString, myListType);
Type myListType=new-TypeToken(){}.getType();
List myList=new Gson().fromJson(jsonString,myListType);
您的json是否完整?因为它看起来像来自“[”和“]”的jsonArray。你有无效的JSON,它告诉你这一点。除此之外,你最初是如何使用Gson的,这是值得怀疑的。如果您想要一个JsonObject
而不是POJO的映射,请参阅感谢Brian和Amrut的回复。是的,这是一个完整的JSON。我想我错过了JSONParser,我要试试。这是一个JSON数组,不是JSON对象,那么为什么要尝试反序列化到JsonObject呢?还有,为什么要费劲地把它全吐出来,然后把它给Gson呢?葛森可以为你做那件事你什么意思?gson不需要字符串吗?
public class ResponseObject {
public String email;
public long timestamp;
public String event;
public String url;
public String userid;
public String template;
}
Type myListType = new TypeToken<List<ResponseObject>>(){}.getType();
List<ResponseObject> myList = new Gson().fromJson(jsonString, myListType);