简体   繁体   English

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

[英]Getting an error when trying to deserialize a JSON string with GSON

I'm trying to decode a JSON string that's coming from a php script I made that gets results out of my MySQL database. 我正在尝试解码来自我制作的php脚本的JSON字符串,该字符串从MySQL数据库中获取结果。 It returns a JSON array. 它返回一个JSON数组。

This is the code for my decoder: 这是我的解码器的代码:

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());
        }

    }   
}

This is the Pin object class: 这是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;
}

} }

This is the JSON String that I'm trying to decode: 这是我要解码的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"
      }
]

And this is how I make the Json string in PHP 这就是我在PHP中制作Json字符串的方式

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

echo json_encode($pins); 

Whenever I run my app, it gives me this error: 每当我运行我的应用程序时,都会出现此错误:

java.lang.NullPointerException: println needs a message

Anyone knows what I am doing wrong? 有人知道我在做什么错吗?

If you need any more information, please do ask! 如果您需要更多信息,请询问!

Thanks in advance! 提前致谢!

Gson tries to match the JSON element names to your class' field names. Gson尝试将JSON元素名称与您的类的字段名称匹配。 In your case, you have id , title , etc. instead of pinId , pinTitle , etc. If Gson finds an element for which it doesn't find a matching field, it skips it, leaving the field null (or whatever default it has). 在您的情况下,您具有idtitle等,而不是pinIdpinTitle等。如果Gson找到一个找不到匹配字段的元素,它将跳过该元素,使该字段为null (或具有它的任何默认值) )。

The element and field names need to be equal. 元素名称和字段名称必须相等。

Alternatively, you could annotate your field with @SerializedName and give it the value you are expecting from the json. 或者,您可以使用@SerializedName注释字段,并@SerializedName提供json中期望的值。

@SerializedName("id")
private Integer pinID;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM