Java 尝试使用GSON反序列化JSON字符串时出错

Java 尝试使用GSON反序列化JSON字符串时出错,java,php,json,gson,Java,Php,Json,Gson,我试图解码一个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.Ty

我试图解码一个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());
        }

    }   
}
}

这是我试图解码的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元素名与类的字段名相匹配。在您的情况下,您有
id
title
等,而不是
pinId
pinTitle
等。如果Gson发现一个元素没有找到匹配的字段,它会跳过该元素,将该字段保留为
null
(或任何默认值)

元素名和字段名必须相等

或者,您可以使用
@SerializedName
对字段进行注释,并为其提供json中期望的值

@SerializedName("id")
private Integer pinID;
@SerializedName("id")
private Integer pinID;