7

ここで非常に簡単な問題があります。API から取得した JSON を取得し、作成したオブジェクトに変換する必要があります。

これまでのところ、リストに逆シリアル化されますが、各メトリック オブジェクトには null 値があります

入ってくるJSON

{
"metrics": [
    {
        "metric": {
            "type": 1,
            "name": "slide-11-start",
            "value": "1287249598295",
            "sessionID": "" 
        } 
    },
    {
        "metric": {
            "type": 1,
            "name": "slide-21-start",
            "value": "1287249601368",
            "sessionID": "" 
        } 
    },
    {
        "metric": {
            "type": 7,
            "name": "resolution",
            "value": "1680x1050",
            "sessionID": "" 
        } 
    },
    {
        "metric": {
            "type": 6,
            "name": "OS",
            "value": "Linux",
            "sessionID": "" 
        } 
    },
    {
        "metric": {
            "type": 5,
            "name": "browser",
            "value": "Netscape",
            "sessionID": "" 
        } 
    } 
]

}

メトリック オブジェクト

public class Metric {

    private int type;
    private String name;
    private String value;
    private String sessionID;

    /**
     * @return the type
     */
    public int getType() {
        return type;
    }

    /**
     * @param type the type to set
     */
    public void setType(int type) {
        this.type = type;
    }

    /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * @return the value
     */
    public String getValue() {
        return value;
    }

    /**
     * @param value the value to set
     */
    public void setValue(String value) {
        this.value = value;
    }

    /**
     * @return the sessionID
     */
    public String getSessionID() {
        return sessionID;
    }

    /**
     * @param sessionID the sessionID to set
     */
    public void setSessionID(String sessionID) {
        this.sessionID = sessionID;
    }

}

コンテナ オブジェクト

import java.util.List;

/**
 *
 * @author joshua
 */
public class MetricSet {

    private List<Metric> metrics;

    /**
     * @return the metrics
     */
    public List<Metric> getMetrics() {
        return metrics;
    }

    /**
     * @param metrics the metrics to set
     */
    public void setMetrics(List<Metric> metrics) {
        this.metrics = metrics;
    }
}

JSON を変換するコード

    String json = "";
    if(request.getParameter("data") != null) {
        json = request.getParameter("data");
    }

    MetricSet metrics = new MetricSet();

    try {
        Gson gson = new Gson();
        Type listType = new TypeToken<MetricSet>() {}.getType();
        metrics = gson.fromJson(json, MetricSet.class);
    }
    catch(Exception ex) {
        String msg = ex.toString();
    }
4

2 に答える 2

8

JSON オブジェクトに対応する Java クラスを作成できます。、値はそのままマッピングできますintegerstringJSON は次のように解析できます。

 Gson gson = new GsonBuilder().create();
 Response r = gson.fromJson(jsonString, Response.class);

例を次に示します: http://rowsandcolumns.blogspot.com/2013/02/url-encode-http-get-solr-request-and.html

于 2013-02-11T19:04:23.023 に答える
2

(7.5ヶ月しか経っていません。)

問題は、逆シリアル化しようとしているクラス構造が JSON 構造と一致しないことです。

以下は、提供された JSON を逆シリアル化するために機能する簡単な例です。出力は

[指標: [
[MetricContainer: [メトリック: type=1, name=slide-11-start, value=1287249598295, sessionID=]],
[MetricContainer: [メトリック: type=1, name=slide-21-start, value=1287249601368, sessionID=]],
[MetricContainer: [メトリック: タイプ=7、名前=解像度、値=1680x1050、セッションID=]]、
[MetricContainer: [メトリック: タイプ=6、名前=OS、値=Linux、セッションID=]]、
[MetricContainer: [メトリック: タイプ=5、名前=ブラウザ、値=Netscape、セッションID=]]]]
public class Foo
{
  static String jsonInput = 
    "{" + 
      "\"metrics\":" +
      "[" + 
        "{" + 
          "\"metric\":" +
          "{" + 
            "\"type\":1," + 
            "\"name\":\"slide-11-start\"," + 
            "\"value\":\"1287249598295\"," + 
            "\"sessionID\":\"\"" + 
          "}" + 
        "}," + 
        "{" + 
          "\"metric\":" +
          "{" + 
            "\"type\":1," + 
            "\"name\":\"slide-21-start\"," + 
            "\"value\":\"1287249601368\"," + 
            "\"sessionID\":\"\"" + 
          "}" + 
        "}," + 
        "{" + 
          "\"metric\":" +
          "{" + 
            "\"type\":7," + 
            "\"name\":\"resolution\"," + 
            "\"value\":\"1680x1050\"," + 
            "\"sessionID\":\"\"" + 
          "}" + 
        "}," + 
        "{" + 
          "\"metric\":" +
          "{" + 
            "\"type\":6," + 
            "\"name\":\"OS\"," + 
            "\"value\":\"Linux\"," + 
            "\"sessionID\":\"\"" + 
          "}" + 
        "}," + 
        "{" + 
          "\"metric\":" +
          "{" + 
            "\"type\":5," + 
            "\"name\":\"browser\"," + 
            "\"value\":\"Netscape\"," + 
            "\"sessionID\":\"\"" + 
          "}" + 
        "}" +  
      "]" + 
    "}";

  public static void main(String[] args)
  {
    GsonBuilder gsonBuilder = new GsonBuilder();
    Gson gson = gsonBuilder.create();
    Metrics metrics = gson.fromJson(jsonInput, Metrics.class);
    System.out.println(metrics);
  }
}

class Metrics 
{
  private List<MetricContainer> metrics;

  @Override
  public String toString()
  {
    return String.format(
        "[Metrics: %1$s]", 
        metrics);
  }
}

class MetricContainer
{
  private Metric metric;

  @Override
  public String toString()
  {
    return String.format(
        "[MetricContainer: %1$s]", 
        metric);
  }
}

class Metric 
{  
  private int type;
  private String name;
  private String value;
  private String sessionID;

  @Override
  public String toString()
  {
    return String.format(
        "[Metric: type=%1$d, name=%2$s, value=%3$s, sessionID=%4$s]", 
        type, name, value, sessionID);
  }
}
于 2011-06-01T11:03:16.847 に答える