英文:
Error while trying to fetch queryresult.KV object in JSON.Unmarshal
问题
我在这里有点困惑,尽管我已经进行了很多搜索,但我的知识中显然还有一些缺失,所以我想请你帮助我。
我创建了一个Hyperledger Fabric网络,并在其中安装了一个链码。我想创建一个函数,用于检索有关键的所有World State输入。我已经使用bytes.Buffer
完成了这个功能,并且它也起作用了。但是我想用一个结构体来实现。
所以,我创建了以下只包含键的结构体:
type WSKeys struct {
Key string `json:"key"`
Namespace string `json:"Namespace"`
}
这是我的代码函数:
func (s *SmartContract) getAllWsDataStruct(APIstub shim.ChaincodeStubInterface, args []string) sc.Response {
var keyArrayStr []WSKeys
resultsIterator, err := APIstub.GetQueryResult("{\"selector\":{\"_id\":{\"$ne\": null }}}")
if err != nil {
return shim.Error("Error occured when trying to fetch data: " + err.Error())
}
for resultsIterator.HasNext() {
// Get the next record
queryResponse, err := resultsIterator.Next()
if err != nil {
return shim.Error(err.Error())
}
fmt.Println(queryResponse)
var qry_key_json WSKeys
json.Unmarshal([]byte(queryResponse), &qry_key_json)
keyArray = append(keyArray, qry_key_json)
}
defer resultsIterator.Close()
all_bytes, _ := json.Marshal(keyArray)
fmt.Println(keyArray)
return shim.Success(all_bytes)
}
当执行上述代码时,我得到以下错误:
cannot convert queryResponse (type *queryresult.KV) to type []byte
如果我执行以下操作,我可以正确地获取结果:
func (s *SmartContract) getAllWsDataStruct(APIstub shim.ChaincodeStubInterface, args []string) sc.Response {
var keyArray []string
resultsIterator, err := APIstub.GetQueryResult("{\"selector\":{\"_id\":{\"$ne\": null }}}")
if err != nil {
return shim.Error("Error occured when trying to fetch data: " + err.Error())
}
for resultsIterator.HasNext() {
// Get the next record
queryResponse, err := resultsIterator.Next()
if err != nil {
return shim.Error(err.Error())
}
fmt.Println(queryResponse)
keyArray = append(keyArray, queryResponse.Key)
}
defer resultsIterator.Close()
all_bytes, _ := json.Marshal(keyArray)
fmt.Println(keyArray)
return shim.Success(all_bytes)
}
但是,为什么当我尝试将queryResponse添加到自定义结构体中时会出现上述错误?我需要将其添加到仅为其类型的结构体中吗?
请问有人可以解释一下我在这里漏掉了什么吗?
英文:
I am a little bit confused here and although I have searched a lot on this, something is clearly missing from my knowledge and I am asking your help.
I have created a Hyperledger Fabric Network and installed a chaincode in it. And I want to make a function that retrieves all the World State inputs about the Keys. I have done it already with the bytes.Buffer
and it worked. But what I want to do is to do it with a struct.
So, I created the following struct that has only the key:
type WSKeys struct {
Key string `json: "key"`
Namespace string `json: "Namespace"`
}
And this is my code function:
func (s *SmartContract) getAllWsDataStruct(APIstub shim.ChaincodeStubInterface , args []string) sc.Response {
var keyArrayStr []WSKeys
resultsIterator, err := APIstub.GetQueryResult("{\"selector\":{\"_id\":{\"$ne\": null }} }")
if err != nil {
return shim.Error("Error occured when trying to fetch data: "+err.Error())
}
for resultsIterator.HasNext() {
// Get the next record
queryResponse, err := resultsIterator.Next()
if err != nil {
return shim.Error(err.Error())
}
fmt.Println(queryResponse)
var qry_key_json WSKeys
json.Unmarshal([]byte(queryResponse), &qry_key_json)
keyArray = append(keyArray, qry_key_json)
}
defer resultsIterator.Close()
all_bytes, _ := json.Marshal(keyArray)
fmt.Println(keyArray)
return shim.Success(all_bytes)
}
When executing the above I get the following error:
cannot convert queryResponse (type *queryresult.KV) to type []byte
I can get the results correctly if I, for example do this:
func (s *SmartContract) getAllWsDataStruct(APIstub shim.ChaincodeStubInterface , args []string) sc.Response {
var keyArray []string
resultsIterator, err := APIstub.GetQueryResult("{\"selector\":{\"_id\":{\"$ne\": null }} }")
if err != nil {
return shim.Error("Error occured when trying to fetch data: "+err.Error())
}
for resultsIterator.HasNext() {
// Get the next record
queryResponse, err := resultsIterator.Next()
if err != nil {
return shim.Error(err.Error())
}
fmt.Println(queryResponse)
keyArray = append(keyArray, queryResponse.Key)
}
defer resultsIterator.Close()
all_bytes, _ := json.Marshal(keyArray)
fmt.Println(keyArray)
return shim.Success(all_bytes)
}
But, why I get the above error when trying to add the queryResponse into a custom struct?
Do I need to add it to a struct that is only its type?
Please someone can explain what I am missing here?
答案1
得分: 1
错误提示已经足够详细,表明您的[]byte
转换失败,失败的类型是queryResponse
,经过一些查找,它似乎是一个结构体类型。在Go语言中,您不能直接将结构体实例转换为其组成的字节,除非使用gob或其他方式进行编码。
也许您的意图是在结构体中使用Key
记录进行解组:
json.Unmarshal([]byte(queryResponse.Key), &qry_key_json)
英文:
The error statement is verbose enough to indicate, that your []byte
conversion failed for the type queryResponse
which, with a bit of lookup seems to be a struct type. In Go you cannot natively convert a struct instance to its constituent bytes without encoding using gob or other means.
Perhaps your intention was to use the Key
record in the struct for un-marshalling
json.Unmarshal([]byte(queryResponse.Key), &qry_key_json)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论