使用Java读取/解析JSON

时间:2012-02-07 22:15:31

标签: java json parsing gson

我从以下YAHOO网址获得了一个JSON对象,它会使用查询返回股票代码。网址是。

    http://d.yimg.com/autoc.finance.yahoo.com/autoc?query=Siemens&callback=YAHOO.Finance.SymbolSuggest.ssCallback

当我使用在线json验证器时,看来YAHOO返回的JSON对象不是100%有效。

网站

http://jsonformatter.curiousconcept.com/

告诉我,JSON对象无效。所以我决定使用YAHOO webservice的子字符串,它是一个有效的JSON对象。在这种情况下,我正在使用输出

{"ResultSet":{"Query":"siemens","Result":[{"symbol":"SI", ... }]}}

使用这个子字符串我的所有验证器告诉我,我在这里有一个有效的JSON对象。

我的Java代码,我正在尝试反序列化我的JSON对象,如下所示:

import java.util.List;

public class ResultSet {
private String query;
private List<Result> result;

public String getQuery() {
    return this.query;
}

public void setQuery(String query) {
    this.query = query;
}

public List<Result> getResult() {
    return this.result;
}

public void setResult(List<Result> result) {
    this.result = result;
}
}

public class Result {
private String exch;
private String exchDisp;
private String name;
private String symbol;
private String type;
private String typeDisp;

public String getExch() {
    return this.exch;
}

public void setExch(String exch) {
    this.exch = exch;
}

public String getExchDisp() {
    return this.exchDisp;
}

public void setExchDisp(String exchDisp) {
    this.exchDisp = exchDisp;
}

public String getName() {
    return this.name;
}

public void setName(String name) {
    this.name = name;
}

public String getSymbol() {
    return this.symbol;
}

public void setSymbol(String symbol) {
    this.symbol = symbol;
}

public String getType() {
    return this.type;
}

public void setType(String type) {
    this.type = type;
}

public String getTypeDisp() {
    return this.typeDisp;
}

public void setTypeDisp(String typeDisp) {
    this.typeDisp = typeDisp;
}
}

我的代码,我试图用谷歌的GSON反序列化对象看起来像这样。

public String getTickerSymbol() {
    String content = new URLContentLoader(url).getContent();
    content = content.substring(43, content.length()-1);

    ResultSet data = new Gson().fromJson(content, ResultSet.class);

    System.out.println(">>" + data.getResult());

    return null;
}

名为“content”的变量的内容是

{"ResultSet":{"Query":"siemens","Result":[{"symbol":"SI","name": "Siemens AG","exch": "NYQ","type": "S","exchDisp":"NYSE","typeDisp":"Equity"},{"symbol":"SIE.DE","name": "Siemens AG","exch": "GER","type": "S","exchDisp":"XETRA","typeDisp":"Equity"},{"symbol":"SIE.MU","name": "SIEMENS N","exch": "MUN","type": "S","exchDisp":"Munich","typeDisp":"Equity"},{"symbol":"SIEMENS.NS","name": "Siemens Ltd.","exch": "NSI","type": "S","exchDisp":"NSE","typeDisp":"Equity"},{"symbol":"SIE.MI","name": "Siemens AG","exch": "MIL","type": "S","exchDisp":"Milan","typeDisp":"Equity"},{"symbol":"SIE.F","name": "SIEMENS N","exch": "FRA","type": "S","exchDisp":"Frankfurt","typeDisp":"Equity"},{"symbol":"SIEMENS.BO","name": "Siemens Ltd.","exch": "BSE","type": "S","exchDisp":"Bombay","typeDisp":"Equity"},{"symbol":"SIEB.SG","name": "SIEMENS SP.ADR","exch": "STU","type": "S","exchDisp":"Stuttgart","typeDisp":"Equity"},{"symbol":"SIEB.DE","name": "Siemens AG","exch": "GER","type": "S","exchDisp":"XETRA","typeDisp":"Equity"},{"symbol":"SIE.L","name": "Siemens AG","exch": "LSE","type": "S","exchDisp":"London","typeDisp":"Equity"}]}}

函数调用data.getResult()返回值“null”。函数getTickerSymbol的输出打印出以下控制台输出。

>>null

有人知道为什么对象没有以正确的方式反序列化吗?

我的目标是使用YAHOO网络服务来检索公司的股票代码,以便接收他们的股票报价。

主要问题是资本化。 Q uery是用大写的Q编写的。在ResultSet类中,它是用 q 编写的。数据绑定机制无法处理这种开箱即用的问题。使用jackson,您可以使用Annotations,这样您就不必修改JSON-Object和原始变量名。在Java中,使用大写变量名称不是通用代码样式。使用Jackson Annotions,您可以手动进行映射。它有效!

使用以下代码,我得到了它的工作。感谢您的建议: - )

使用Jackson lib解决方案

import java.util.List;
import org.codehaus.jackson.annotate.JsonProperty;

public class ResultSet {
private String query;
private List<Result> result;

public String getQuery() {
    return this.query;
}

@JsonProperty("Query")
public void setQuery(String query) {
    this.query = query;
}

public List<Result> getResult() {
    return this.result;
}

@JsonProperty("Result")
public void setResult(List<Result> result) {
    this.result = result;
}
}

帮助程序类代码

import org.codehaus.jackson.map.ObjectMapper;

public class JsonUtils {
public static <T> T parseJson(String json, Class<T> resultType) {
    ObjectMapper objectMapper = new ObjectMapper();
    try {
        return objectMapper.readValue(json, resultType);
    } catch (Exception e) {
        System.err.println(e.getMessage());
    }
    return null;
}
}

构建对象

ResultSet rs = JsonUtils.parseJson(content, ResultSet.class);

for ( Result result : rs.getResult() ) {
    System.out.println(result.getSymbol());
}

2 个答案:

答案 0 :(得分:2)

这是因为ResultSet.class并不真正对应给定的JSON。不幸的是,你需要有另一个类作为ResultSet的容器​​:

public class JsonContainer {

  private ResultSet jsonContainer;

  // getter/setter
}

然后你需要这样做:

JsonContainer data = new Gson().fromJson(content, JsonContainer.class);

如果这不起作用,则必须使用驼峰案例:将private ResultSet jsonContainer;更改为private ResultSet JsonContainer;或使用@SerializedName

答案 1 :(得分:2)

问题是ResultSet类中没有名为ResultSet的属性。您应该反序列化此子字符串:

{"Query":"siemens","Result":[{"symbol":"SI","name": "Siemens AG","exch": "NYQ","type": "S","exchDisp":"NYSE","typeDisp":"Equity"},{"symbol":"SIE.DE","name": "Siemens AG","exch": "GER","type": "S","exchDisp":"XETRA","typeDisp":"Equity"},{"symbol":"SIE.MU","name": "SIEMENS N","exch": "MUN","type": "S","exchDisp":"Munich","typeDisp":"Equity"},{"symbol":"SIEMENS.NS","name": "Siemens Ltd.","exch": "NSI","type": "S","exchDisp":"NSE","typeDisp":"Equity"},{"symbol":"SIE.MI","name": "Siemens AG","exch": "MIL","type": "S","exchDisp":"Milan","typeDisp":"Equity"},{"symbol":"SIE.F","name": "SIEMENS N","exch": "FRA","type": "S","exchDisp":"Frankfurt","typeDisp":"Equity"},{"symbol":"SIEMENS.BO","name": "Siemens Ltd.","exch": "BSE","type": "S","exchDisp":"Bombay","typeDisp":"Equity"},{"symbol":"SIEB.SG","name": "SIEMENS SP.ADR","exch": "STU","type": "S","exchDisp":"Stuttgart","typeDisp":"Equity"},{"symbol":"SIEB.DE","name": "Siemens AG","exch": "GER","type": "S","exchDisp":"XETRA","typeDisp":"Equity"},{"symbol":"SIE.L","name": "Siemens AG","exch": "LSE","type": "S","exchDisp":"London","typeDisp":"Equity"}]}