gpt4 book ai didi

java - 尝试使用 GSON 反序列化 JSON 字符串时出现错误

转载 作者:行者123 更新时间:2023-12-01 13:40:40 25 4
gpt4 key购买 nike

我正在尝试解码来 self 制作的 PHP 脚本的 JSON 字符串,该脚本从我的 MySQL 数据库中获取结果。它返回一个 JSON 数组。

这是我的解码器的代码:

package com.github.viperdream;

import java.lang.reflect.Type;
import java.util.List;

import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

public class JSONDecoder {

public static void decodePin(String data){

Gson gson = new Gson();

Type type = new TypeToken<List<Pin>>(){}.getType();
List<Pin> pin = gson.fromJson(data, type);

for (Pin newPin : pin){
Log.d("GSON", newPin.getPinTitle());
}

}
}

这是 Pin 对象类:

package com.github.viperdream;

public class Pin {

private Integer pinID;
private String pinTitle;
private String pinMessage;
private Integer pinDuration;
private Float pinX;
private Float pinY;
private String pinColor;
private String author;
private Integer pinSQLId;

public Pin(){}

public Pin(int pinID, String pinTitle, String pinMessage, Integer pinDuration, Float pinX, Float pinY, String pinColor, String author, Integer pinSQLId){
this.pinID = pinID;
this.pinTitle = pinTitle;
this.pinMessage = pinMessage;
this.pinDuration = pinDuration;
this.pinX = pinX;
this.pinY = pinY;
this.pinColor = pinColor;
this.author = author;
this.pinSQLId = pinSQLId;
}

public Pin(String pinTitle, String pinMessage, Integer pinDuration, Float pinX, Float pinY, String pinColor, String author, Integer pinSQLId){
this.pinTitle = pinTitle;
this.pinMessage = pinMessage;
this.pinDuration = pinDuration;
this.pinX = pinX;
this.pinY = pinY;
this.pinColor = pinColor;
this.author = author;
this.pinSQLId = pinSQLId;
}

//get ------------------------------------------------------
public int getID(){
return this.pinID;
}

public String getPinTitle(){
return this.pinTitle;
}

public String getPinMessage(){
return this.pinMessage;
}

public Integer getPinDuration(){
return this.pinDuration;
}
public Float getPinX(){
return this.pinX;
}

public Float getPinY(){
return this.pinY;
}
public String getPinColor(){
return this.pinColor;
}
public String getPinAuthor(){
return this.author;
}
public Integer getPinSQLId(){
return this.pinSQLId;
}

//set ------------------------------------------------------
public void setPinID(int pinID){
this.pinID = pinID;
}

public void setPinTitle(String pinTitle){
this.pinTitle = pinTitle;
}

public void setPinMessage(String pinMessage){
this.pinMessage = pinMessage;
}

public void setPinDuration(int pinDuration){
this.pinDuration = pinDuration;
}
public void setPinX(Float pinX){
this.pinX = pinX;
}

public void setPinY(Float pinY){
this.pinY = pinY;
}
public void setPinColor(String pinColor){
this.pinColor = pinColor;
}
public void setPinAuthor(String author){
this.author = author;
}
public void setPinSQLId(Integer pinSQLId){
this.pinSQLId = pinSQLId;
}

}

这是我尝试解码的 JSON 字符串:

[
{
"id":"2",
"title":"test1",
"message":"test2",
"duration":"1",
"x":"125",
"y":"754.5",
"color":"red",
"author":"viperdream"
},
{
"id":"3",
"title":"looking for someone",
"message":"i need to go now",
"duration":"1",
"x":"401",
"y":"472.5",
"color":"red",
"author":"viperdream"
},
{
"id":"4",
"title":"test3",
"message":"testing:)",
"duration":"1",
"x":"195",
"y":"512.5",
"color":"red",
"author":"viperdream"
}
]

这就是我在 PHP 中制作 Json 字符串的方法

while($pin = mysql_fetch_assoc($query_exec)) {
$pins[] = $pin;
}

echo json_encode($pins);

每当我运行我的应用程序时,都会出现以下错误:

java.lang.NullPointerException:println需要一条消息

有人知道我做错了什么吗?

如果您需要更多信息,请询问!

提前致谢!

最佳答案

Gson 尝试将 JSON 元素名称与类的字段名称相匹配。在你的情况下,你有 idtitle 等,而不是 pinIdpinTitle 等。如果 Gson找到一个没有找到匹配字段的元素,它会跳过它,保留字段 null (或它具有的任何默认值)。

元素名称和字段名称必须相同。

或者,您可以使用 @SerializedName 注释您的字段,并为其提供您期望从 json 中获得的值。

@SerializedName("id")
private Integer pinID;

关于java - 尝试使用 GSON 反序列化 JSON 字符串时出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20819576/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com