0

この文字列を取得して String Array または HashMap に入れる方法は? 参照はありますか?

     {
        "code" : 403,
            "errors" : [ {
                "domain" : "global",
                "location" : "Authorization",
                "locationType" : "header",
                "message" : "The domain policy has disabled third-party Drive apps",
                "reason" : "domainPolicy"
            } ],
        "message" : "The domain policy has disabled third-party Drive apps"
     }

====

これはジャクソンの私の解決策です:

public class TheErrorModel {
    public int code; 
    public Collection<Error> errors; 
    public String message;

    public TheErrorModel() {
    }

    public TheErrorModel(String json) 
            throws Exception {
        ObjectMapper mapper = new ObjectMapper();
        TheErrorModelother = mapper.readValue(json, TheErrorModel.class);

        this.code = other.code;
        this.errors = other.errors;
        this.message = other.message;
    }
}

class Error {
    public String domain;
    public String location;
    public String locationType; 
    public String message;
    public String reason;
}

TheErrorModel theErrorModel = new TheErrorModel(json);

だから私は theErrorModel.message のようなものを受け取ります:D

4

2 に答える 2

2

GSON http://code.google.com/p/google-gson/を試すことができます。使い方はとても簡単です。この json スキーマのクラス構造を作成するだけです。

import com.google.gson.Gson;
class YourErrorsJSON{
  private String domain 
  private String location;
  private String locationType; 
  private String message; 
  private String reason 
}

class YourJSON {
  private int code; 
  private YourErrorsJSON[] errors; 
  private String message;
}
Gson gson = new Gson();
YourJSON obj = gson.fromJson(json, YourJSON.class);
于 2012-11-30T07:15:40.673 に答える
1

これを試して ::

JSONObject yourObj = JSONObject.fromString(input_string);
String code = yourObj.get("code"); // code will have a value 403
于 2012-11-30T07:07:11.213 に答える