7

我正在使用 Spring Social FqlQuery 从 facebook 获取数据。这是我从 facebook 获得的 JSON 响应。我得到 Json 输出的控制器在这里,

fql = "SELECT work FROM user WHERE uid = me()";
facebook.fqlOperations().query(fql, new FqlResultMapper<Object>() {
    public Object mapObject(FqlResult result) {
        List list = (List) result.getObject("work");
        for (Object object : list) {
           JsonHelper jsonHelper = new JsonHelper();
           Gson gson = new GsonBuilder().setPrettyPrinting().create();
           String jsonOutput = gson.toJson(object);
           System.out.println(jsonOutput);
           gson.fromJson(jsonOutput, JsonHelper.class);
        }

System.out.println inside for循环输出多个json如下:

{
  "employer": {
    "id": 129843057436,
    "name": "www.metroplots.com"
  },
  "location": {
    "id": 102186159822587,
    "name": "Chennai, Tamil Nadu"
  },
  "position": {
    "id": 108480125843293,
    "name": "Web Developer"
  },
  "start_date": "2012-10-01",
  "end_date": "2013-05-31"
}
{
  "employer": {
    "id": 520808381292985,
    "name": "Federation of Indian Blood Donor Organizations"
  },
  "start_date": "0000-00",
  "end_date": "0000-00"
}

这是我的助手类:

import java.util.List;

public class JsonHelper {

class Employer{
    private int id;
    private String name;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }

}
class Location{
    private int id;
    private String name;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }   

}
class Position{
    private int id;
    private String name;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}
//Edited After here
private String start_Date;
private String end_Date;
private Employer employer;
private Location location;
private Position position;
public String getStart_Date() {
    return start_Date;
}
public void setStart_Date(String start_Date) {
    this.start_Date = start_Date;
}
public String getEnd_Date() {
    return end_Date;
}
public void setEnd_Date(String end_Date) {
    this.end_Date = end_Date;
}
public Employer getEmployer() {
    return employer;
}
public void setEmployer(Employer employer) {
    this.employer = employer;
}
public Location getLocation() {
    return location;
}
public void setLocation(Location location) {
    this.location = location;
}
public Position getPosition() {
    return position;
}
public void setPosition(Position position) {
    this.position = position;
}
}

当我尝试如上所述将 json 对象转换为 java 对象时,我得到了这个异常。

HTTP Status 500 - Request processing failed; nested exception is com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 6 column 16

任何人都可以在我错的地方帮助我。请帮我将 json 转换为 java 对象。希望我的问题很清楚。提前致谢。

对控制器进行的编辑:

facebook.fqlOperations().query(fql, new FqlResultMapper<Object>() {
public Object mapObject(FqlResult result) {
List<JsonHelper> json = new ArrayList<JsonHelper>();
List list = (List) result.getObject("work");

for (Object object : list) {
    Gson gson = new GsonBuilder().setPrettyPrinting().create();
    String jsonOutput = gson.toJson(object);
    System.out.println(jsonOutput);
    JsonHelper jsonHelper = gson.fromJson(jsonOutput, JsonHelper.class);
    json.add(jsonHelper);
    System.out.println(jsonHelper.getStart_Date());
}

for (JsonHelper jsonHelper : json) {
    System.out.println(jsonHelper.getStart_Date());
}

return list;
}

});
4

1 回答 1

1

由于我没有实际的 api 访问权限,所以我在示例中尝试使用静态值。首先在您的JsonHelper班级中,将所有内容替换intlong,因为 json 中提到的值的类型是longString。然后尝试如下所述:

            String str = "{\n"
            + "  \"employer\": {\n"
            + "    \"id\": 129843057436,\n"
            + "    \"name\": \"www.metroplots.com\"\n"
            + "  },\n"
            + "  \"location\": {\n"
            + "    \"id\": 102186159822587,\n"
            + "    \"name\": \"Chennai, Tamil Nadu\"\n"
            + "  },\n"
            + "  \"position\": {\n"
            + "    \"id\": 108480125843293,\n"
            + "    \"name\": \"Web Developer\"\n"
            + "  },\n"
            + "  \"start_date\": \"2012-10-01\",\n"
            + "  \"end_date\": \"2013-05-31\"\n"
            + "}";


    List<JsonHelper> json = new ArrayList<JsonHelper>();
    Gson gson = new Gson();
    JsonHelper users = gson.fromJson(str, JsonHelper.class);
    json.add(users);


    for (JsonHelper js_obj : json) {
        System.out.println(js_obj.getEmployer().getId());
        System.out.println(js_obj.getEmployer().getName());
    }
于 2013-10-28T09:07:08.277 回答