简体   繁体   中英

com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 2 column 5

I have found a lot of answers for this exception, but coudln't find one that helps my case.

I have a string:

"x":{
  "ver":1,
  "size":258,
  "inputs":[
     {
        "sequence":4294967295,
        "prev_out":{
           "spent":true,
           "tx_index":72163587,
           "type":0,
           "addr":"16XEHjFH8bKzSCLhapg6SBvhCoM1YNJeaZ",
           "value":2253421875,
           "n":1,
           "script":"76a9143c90334c94bc129e48c6c793cd54632977250b8088ac"
        },
        "script":"4830450221008cf79d6070885906535f07260100bf41d807323dd653d132019683af29adaac7022061950465a42d3849961a58090e6411e21f13c23b21c52570550f3cee0e678c27014104624d706a244fc9140f8457199e07da3b8b47b385823262b9e0e9e8ab67970712e998bcb9d7f98d2294ebbff5d20852cbd006af59af62365746891b49dd0ffbc9"
     }
  ],
  "time":1418836999,
  "tx_index":72171095,
  "vin_sz":1,
  "hash":"af7c604d6cb0a8f2af0ea6ed90d9eceea88557815f6057f3f7c5438fb1e7f722",
  "vout_sz":2,
  "relayed_by":"127.0.0.1",
  "out":[
     {
        "spent":false,
        "tx_index":72171095,
        "type":0,
        "addr":"153D9XaEbiVu2JH9HtRukra254RHKm6s3M",
        "value":1500000000,
        "n":0,
        "script":"76a9142c4b7991b8b3173b188d5341b1c7f468b45157c588ac"
     },
     {
        "spent":false,
        "tx_index":72171095,
        "type":0,
        "addr":"16XEHjFH8bKzSCLhapg6SBvhCoM1YNJeaZ",
        "value":753418875,
        "n":1,
        "script":"76a9143c90334c94bc129e48c6c793cd54632977250b8088ac"
     }
  ] 
}

When I try to parse this string into a bean: ToolBox.getGson().fromJson(message, Transaction.class);

I get the following exception:

com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 2 column 5

Transaction class is as follows:

public class Transaction {

    private long time;
    private Input[] inputs;
    private long vout_sz;
    private String relayed_by;
    private long vin_sz;
    private long tx_index;
    private int ver;
    private Out[] out;
    private long size;
    private long result;
    private long block_height;
    private String note;
    private String hash;

    public long getTime() {
        return time;
    }
    public void setTime(long time) {
        this.time = time;
    }
    public Input[] getInputs() {
        return inputs;
    }
    public void setInputs(Input[] inputs) {
        this.inputs = inputs;
    }
    public long getVout_sz() {
        return vout_sz;
    }
    public void setVout_sz(long vout_sz) {
        this.vout_sz = vout_sz;
    }
    public String getRelayed_by() {
        return relayed_by;
    }
    public void setRelayed_by(String relayed_by) {
        this.relayed_by = relayed_by;
    }
    public long getVin_sz() {
        return vin_sz;
    }
    public void setVin_sz(long vin_sz) {
        this.vin_sz = vin_sz;
    }
    public long getTx_index() {
        return tx_index;
    }
    public void setTx_index(long tx_index) {
        this.tx_index = tx_index;
    }
    public int getVer() {
        return ver;
    }
    public void setVer(int ver) {
        this.ver = ver;
    }
    public Out[] getOut() {
        return out;
    }
    public void setOut(Out[] out) {
        this.out = out;
    }
    public long getSize() {
        return size;
    }
    public void setSize(long size) {
        this.size = size;
    }
    public long getResult() {
        return result;
    }
    public void setResult(long result) {
        this.result = result;
    }
    public long getBlock_height() {
        return block_height;
    }
    public void setBlock_height(long block_height) {
        this.block_height = block_height;
    }
    public String getNote() {
        return note;
    }
    public void setNote(String note) {
        this.note = note;
    }
    public String getHash() {
        return hash;
    }
    public void setHash(String hash) {
        this.hash = hash;
    }
}

Input class(an array of which resides in the Transaction class)

public class Input {

    private PrevOut prev_out;
    private String script;
    private long sequence;

    public PrevOut getPrev_out() {
        return prev_out;
    }

    public void setPrev_out(PrevOut prev_out) {
        this.prev_out = prev_out;
    }

    public String getScript() {
        return script;
    }

    public void setScript(String script) {
        this.script = script;
    }

    public long getSequence() {
        return sequence;
    }

    public void setSequence(long sequence) {
        this.sequence = sequence;
    }

}

Out class(an array of which resides in the Transaction class)

public class Out {

    private boolean spent;
    private long n;
    private long value;
    private String addr;
    private long tx_index;
    private int type;
    private String script;

    public boolean getSpent() {
        return spent;
    }
    public void setSpent(boolean spent) {
        this.spent = spent;
    }
    public long getN() {
        return n;
    }
    public void setN(long n) {
        this.n = n;
    }
    public long getValue() {
        return value;
    }
    public void setValue(long value) {
        this.value = value;
    }
    public String getAddr() {
        return addr;
    }
    public void setAddr(String addr) {
        this.addr = addr;
    }
    public long getTx_index() {
        return tx_index;
    }
    public void setTx_index(long tx_index) {
        this.tx_index = tx_index;
    }
    public int getType() {
        return type;
    }
    public void setType(int type) {
        this.type = type;
    }
    public String getScript() {
        return script;
    }
    public void setScript(String script) {
        this.script = script;
    }
}

What am I missing? How can I parse the string into the Transaction class?

Your JSON String is not a valid JSON Object. It needs to start and end with { and } . You can run it through a JSON validator like JSONLint and see your problem.

{ 
    "x":{
    ...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM