如何将JSON对象(从google places API返回)转换为Java对象

时间:2012-08-29 15:44:14

标签: java json gson google-places-api unmarshalling

谷歌API在申请食品类别下的地方时会返回一个JSON,其中包括几个地方的详细信息。

我想创建一个对象数组,其中每个对象都包含特定位置的详细信息。

我已经将GSON库用于我的实现,它适用于虚拟JSON对象,但不适用于Google Place API提供的JSON结果,并且抛出了“JsonSyntaxException”。

我寻找以下事项的解决方案.. 1如何继续使用GSON并使用JSON对象创建我的对象数组或 2还有其他方法可以完成我的任务(仍使用JSON结果)

感谢。

更新
类PlaceObject

import java.util.List;

public class PlaceObject {
    private List<String> results;
} 

JSONconverter类

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import com.google.gson.Gson;

public class JSONconverter {
    public static void main(String[] args) {

Gson gson = new Gson();

try {

    BufferedReader br = new BufferedReader(
        new FileReader("c:\\placeAPI.json"));

    //convert the json string back to object
    PlaceObject obj = gson.fromJson(br, PlaceObject.class);
    //obj.results = null ,when debugged thats the problem
    System.out.println("Result: " + obj);

    } catch (IOException e) {
    e.printStackTrace();
    }

    }
}

JSON的链接

http://www.mediafire.com/?8mmnuxuopimhdnz

3 个答案:

答案 0 :(得分:2)

我喜欢使用gson

顺便说一句。还有另一个相关的thread

答案 1 :(得分:1)

泽西岛客户的文档建议使用杰克逊库(see wiki

答案 2 :(得分:1)

您还可以查看Genson图书馆http://code.google.com/p/genson/。 它提供了与球衣的开箱即用的整合。您只需要在类路径中使用jar。