gpt4 book ai didi

java - com.google.gson.JsonSyntaxException : java. lang.IllegalStateException:应为 BEGIN_OBJECT 但在第 2 行第 5 列为 STRING

转载 作者:行者123 更新时间:2023-11-30 07:04:35 29 4
gpt4 key购买 nike

我已经找到了很多关于此异常的答案,但找不到对我的情况有帮助的答案。

我有一个字符串:

"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"
}
]
}

当我尝试将这个字符串解析为一个 bean 时:ToolBox.getGson().fromJson(消息, Transaction.class);

我得到以下异常:

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

事务类如下:

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;
}
}

输入类(其数组驻留在事务类中)

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 类(其中的数组驻留在 Transaction 类中)

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;
}
}

我错过了什么?如何将字符串解析为 Transaction 类?

最佳答案

您的 JSON 字符串不是有效的 JSON 对象。它需要以 {} 开始和结束。您可以通过像 JSONLint 这样的 JSON validator 运行它看看你的问题。

{ 
"x":{
...
}

关于java - com.google.gson.JsonSyntaxException : java. lang.IllegalStateException:应为 BEGIN_OBJECT 但在第 2 行第 5 列为 STRING,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27531590/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com