我正在尝试解码一个JSON字符串,该字符串来自我创建的php脚本,它从我的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 needs a message
任何人都知道我做错了什么?
如果您需要更多信息,请询问!
提前致谢!
答案 0 :(得分:1)
Gson尝试将JSON元素名称与您的类的字段名称进行匹配。在您的情况下,您有id
,title
等,而不是pinId
,pinTitle
等。如果Gson找到了一个找不到匹配的元素字段,它会跳过它,离开字段null
(或其默认值)。
元素和字段名称必须相等。
或者,您可以使用@SerializedName
为您的字段添加注释,并为其提供您期望从json获得的值。
@SerializedName("id")
private Integer pinID;