15

オブジェクトが JSON として返される次の結果クラスがあります。

public class Result {
    public String objectid;
    public String dtype;
    public String type;
    public String name;
    public String description;

    public Result() {
        this.objectid = "";
        this.dtype= "";
        this.type="";
        this.name= "";
        this.description = "";
    }

    public String getObjectid() {
        return objectid;
    }

    public void setObjectid(String s) {
        this.objectid = s;
    }

    public String getDtype() {
        return dtype;
    }

    public void setDtype(String s) {
        this.dtype= s;
    }

    public String getType() {
        return type;
    }

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

    public String getName() {
        return name;
    }

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

    public String getDescription() {
        return description;
    }

    public void setDescription(String s) {
        this.description = s;
    }

}

メインの.javaを読み取り、HTTP RESPONSEとしてjsonとして返される構成jsonがあります。以下の通りです。

{
        "objectid" : "test",
        "dtype" : "test",
        "type" : "test",
        "name" : "test",
        "description" : "test" // removed
},
{
        "objectid" : "test",
        "dtype" : "test",
        "type" : "test",
        "name" : "test",
        "description" : "test"
}

メイン .java

Gson を使用して、configuration.json ファイルを読み取り、json を返す必要があります。

私のコード:

Gson g = new Gson();
Result r = g.fromJson(res.toString(), Result.class);

ここでres.toString()、configuration.json ファイルの内容を文字列として取得します。

私の問題:

次の例外が発生しています。

例外 com.google.gson.JsonSyntaxException: com.google.gson.stream.MalformedJsonException: JsonReader.setLenient(true) を使用して、行 7 列 3
com.google.gson.JsonSyntaxException: com.google.gson.streamで不正な形式の JSON を受け入れます.MalformedJsonException: JsonReader.setLenient(true) を使用して、行 7、列 3 で不正な形式の JSON を受け入れます

ポインタはありますか?

4

4 に答える 4

24

これが実際の json の場合: ここに余分なカンマとスペルミスがあります。エラーは、json 構文が正しくないことを示しています。したがって、これはおそらく最初に探す場所の 1 つです。

{
            "objectid" : "test",
            "dtype" : "test",
            "type" : "test",
            "name " : "test",
            "description" : "test", //delete this comma
            },
            {
            "objectid" : "test",
            "dtyoe" : "test",  // spelling error
            "type" : "test",
            "name " : "test",
            "description" : "test"
    }

また、2 つのオブジェクトを解析し、そこから 1 つの結果オブジェクトが必要であることを gson に伝えているようです。オブジェクトを個別に解析するか、結果配列が必要であることを gson に伝えることを検討してください。

于 2013-07-25T15:49:36.960 に答える
3

使用する

catch(JsonSyntaxException e)

それ以外の

catch(MalformedJsonException e)

MalformedJsonException は何らかの内部例外であり、JsonSyntaxException は実際にスローされるものであるためです。ここにコードスニペットがあります

            String response="Something";
            JsonElement my_json;
            try {
                my_json=jsonParser.parse(response);
            } catch(JsonSyntaxException e) {
                e.printStackTrace();
                JsonReader reader = new JsonReader(new StringReader(response));
                reader.setLenient(true);
                my_json=jsonParser.parse(reader);
            }
于 2014-01-09T15:55:10.333 に答える