英文:
how to update data of a struct and save it in chaincode?
问题
以下是我翻译好的内容:
以下是我创建了一个保存基本信息的客户结构体的链码摘录。我希望编写一个更新函数,该函数首先根据函数的第一个参数(args[0])获取客户的详细信息,然后根据函数的第二个参数更新/追加Country,并保存更新后的数据。
我做错了什么?另外,我想了解Marshal和Unmarshal的概念。
type Customer struct {
UID string
Name string
Address struct {
StreetNo string
Country string
}
}
func (t *SimpleChaincode) update(stub shim.ChaincodeStubInterface, args []string) ([]byte, error) {
if len(args) != 2 {
return nil, errors.New("参数数量不正确。期望查询键的名称")
}
// args[0] 获取要更新详细信息的客户的UID
// args[1] 需要更新的数据
data, err := stub.GetState(args[0])
if err != nil {
return nil, err
}
json.Unmarshal(data, &uid)
// 追加
data.Country = append(data.Country, args[1])
fmt.Printf("为UID %s 设置风险标志", data)
raw, err := json.Marshal(data)
if err != nil {
return nil, err
}
stub.PutState(data.UID, raw)
return nil, nil
}
希望对你有帮助!
英文:
The following is an excerpt of the chaincode where I have created a customer struct that saves basic info. I wish to write an update function that first fetches the details of the customer with the UID(args[0]) and then appends/updates the Country as per the second arguement of the function update and finally saves the updated data.
What am I doing wrong? Also I would like to know the concept of Marshal and Unmarshal.
type Customer struct {
UID string
Name string
Address struct {
StreetNo string
Country string
}
}
func (t *SimpleChaincode) update(stub shim.ChaincodeStubInterface, args []string) ([]byte, error) {
if len(args) != 2 {
return nil, errors.New("Incorrect number of arguments. Expecting name of the key to query")
}
//args[0] to get the UID of customer whose details to be updated
//args[1] data that needs to be updated
data,err:= stub.GetState(ars[0])
if err != nil {
return nil, err
}
json.Unmarshal(data, &uid)
//append
data.Country= append(data.Country,args[1])
fmt.Printf("Risk Fag set for UID %s",data)
raw, err := json.Marshal(data)
if err != nil {
return nil, err
}
stub.PutState(data.UID, raw)
return nil,nil
}
答案1
得分: 1
在Fabric链码示例中,有一个很好的示例,展示了如何将结构体从JSON进行编组和解组。它演示了如何从链中读取JSON数据,更新结构体的某些字段,然后将JSON数据重新保存到链中。请参考示例中的transferMarble函数:
英文:
There is a good example of marshaling and unmarshaling structs to/from JSON in the Fabric chaincode examples. It demonstrates how to read JSON data from the chain, update certain fields of the struct, and then re-save the JSON data to the chain. See the transferMarble function in the example at:
答案2
得分: 0
你需要提供一个变量,JSON包可以将解码后的数据放入其中。例如,这个变量可以是一个字符串到任意数据类型的映射:
map[string]interface{}
byt := []byte(`{"num":6.13,"strs":["a","b"]}`)
if err := json.Unmarshal(byt, &dat); err != nil {
panic(err)
}
fmt.Println(dat)
结果是:
map[num:6.13 strs:[a b]]
英文:
You need to provide a variable where the JSON package can put the decoded data.For example this will hold a map of strings to arbitrary data types:
map[string]interface{}
byt := []byte(`{"num":6.13,"strs":["a","b"]}`)
if err := json.Unmarshal(byt, &dat); err != nil {
panic(err)
}
fmt.Println(dat)
the result is :
map[num:6.13 strs:[a b]]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论