是golang的新手。我的go方法正在将参数作为包含多个对象的数组接收。问题是要从此数组中获取每个对象并将其编组。以下是代码
type myvehicle struct {
ObjectType string `json:"evType"`
Category string `json:"category"`
Model string `json:"model"`
Chassis string `json:"chassis"`
BatteryType string `json:"batteryType"`
BatteryMake string `json:"batteryMake"`
BatteryModelNo string `json:"batteryModelNo"`
BatteryCapacity string `json:"batteryCapacity"`
NBV string `json:"nbv"`
BIN string `json:"bin"`
MCO string `json:"mco" `
VehicleNumber string `json:"vehicleNumber"`
}
type MyChaincode struct {
}
func (t *MyChaincode) register(stub shim.ChaincodeStubInterface, args []string) pb.Response {
fmt.Printf(args[0])
var ev myvehicle
err := json.Unmarshal([]byte(args[0]), &ev)
if err != nil {
return shim.Error(err.Error())
}
return shim.Success(nil)
此处fmt.Printf(args [0])正在打印此内容:-
[{"category":"category-L1","model":"Tiago","VIN":"123XYZ","bType":"Li-ion","bMake":"xyz","bModelNumber":"123ABC","bCapacity":"xyz","NBV":"xyz","BIN":"123ABC","MCO":"certificate in the form of object"},{"category":"category-L1","model":"Tiago","VIN":"123XYZ","bType":"Li-ion","bMake":"xyz","bModelNumber":"123ABC","bCapacity":"xyz","NBV":"xyz","BIN":"123ABC","MCO":"certificate in the form of object"},{"category":"category-L1","model":"Tiago","VIN":"123XYZ","bType":"Li-ion","bMake":"xyz","bModelNumber":"123ABC","bCapacity":"xyz","NBV":"xyz","BIN":"123ABC","MCO":"certificate in the form of object"}]
如上面寄存器方法中的代码所示,我想从args [0]数组中获取每个对象,并将其编组以执行某些功能。 我想要以下东西:-
func (t *EVChaincode) register(stub shim.ChaincodeStubInterface) pb.Response {
EVs := []myvehicle{
myvehicle{Category: "Toyota", Model: "Prius", Chassis: "2434547e", BatteryType: "ToABC@#moko", BatteryMake: "Li-ion", BatteryModelNo: "123", BatteryCapacity: "anythingfornow", NBV: "123", BIN: "123wer", MCO: "asdf", VehicleNumber: "123455"},
myvehicle{Category: "Ford", Model: "Mustang", Chassis: "2434547", BatteryType: "BrABC@#ad", BatteryMake: "Li-ion", BatteryModelNo: "123", BatteryCapacity: "anythingfornow", NBV: "123", BIN: "123wer", MCO: "asdf", VehicleNumber: "123455"},
myvehicle{Category: "Hyundai", Model: "Tucson", Chassis: "2434547en", BatteryType: "JiABC@#n Soo", BatteryMake: "Li-ion", BatteryModelNo: "123", BatteryCapacity: "anythingfornow", NBV: "123", BIN: "123wer", MCO: "asdf", VehicleNumber: "123455"},
myvehicle{Category: "Volkswagen", Model: "Passat", Chassis: "2434547low", BatteryType: "MaABC@#x", BatteryMake: "Li-ion", BatteryModelNo: "123", BatteryCapacity: "anythingfornow", NBV: "123", BIN: "123wer", MCO: "asdf", VehicleNumber: "123455"},
myvehicle{Category: "Tesla", Model: "S", Chassis: "2434547ck", BatteryType: "AdABC@#riana", BatteryMake: "Li-ion", BatteryModelNo: "123", BatteryCapacity: "anythingfornow", NBV: "123", BIN: "123wer", MCO: "asdf", VehicleNumber: "123455"},
myvehicle{Category: "Peugeot", Model: "205", Chassis: "2434547ple", BatteryType: "MiABC@#chel", BatteryMake: "Li-ion", BatteryModelNo: "123", BatteryCapacity: "anythingfornow", NBV: "123", BIN: "123wer", MCO: "asdf", VehicleNumber: "123455"},
myvehicle{Category: "Chery", Model: "S22L", Chassis: "2434547te", BatteryType: "AaABC@#rav", BatteryMake: "Li-ion", BatteryModelNo: "123", BatteryCapacity: "anythingfornow", NBV: "123", BIN: "123wer", MCO: "asdf", VehicleNumber: "123455"},
myvehicle{Category: "Fiat", Model: "Punto", Chassis: "2434547let", BatteryType: "PaABC@#ri", BatteryMake: "Li-ion", BatteryModelNo: "123", BatteryCapacity: "anythingfornow", NBV: "123", BIN: "123wer", MCO: "asdf", VehicleNumber: "123455"},
myvehicle{Category: "Tata", Model: "Nano", Chassis: "2434547igo", BatteryType: "VaABC@#leria", BatteryMake: "Li-ion", BatteryModelNo: "123", BatteryCapacity: "anythingfornow", NBV: "123", BIN: "123wer", MCO: "asdf", VehicleNumber: "123455"},
myvehicle{Category: "Holden", Model: "Barina", Chassis: "2434547wn", BatteryType: "ShABC@#otaro", BatteryMake: "Li-ion", BatteryModelNo: "123", BatteryCapacity: "anythingfornow", NBV: "123", BIN: "123wer", MCO: "asdf", VehicleNumber: "123455"},
}
fmt.Printf(stub.GetTxID())
fmt.Println("-----------------------------------------------------")
i := 0
for i < len(EVs) {
fmt.Println("i is ", i)
carAsBytes, _ := json.Marshal(EVs[i])
fmt.Println("Added", EVs[i])
i = i + 1
}
return shim.Success(nil)
}
唯一的区别是在第二种情况下不是从参数获取数据。 我希望我已经很好地解释了这个问题