英文:
Gorethink Insert Issue
问题
var data = map[string]interface{}{
"json_received": [
{
"ezpOrderId": "ezp_123",
"firstName": "Vasanth",
"lastName": "K",
"orderDesc": "Sample"
}
],
"created_on": "03-22-2015",
"status": "1"
}
result, err := r.Table("order_json").Insert(data).RunWrite(session)
当我尝试运行这个程序时,我得到了一个错误,错误信息是"json_received"后面缺少操作数。
请帮助我通过Go编程将数据变量插入到RethinkDB中。
英文:
var data = map[string]interface{}{
	"json_received": [
		{
		"ezpOrderId":  "ezp_123",
		"firstName":  "Vasanth",
		"lastName":  "K",
		"orderDesc":  "Sample"
		}
	] 
	"created_on":  "03-22-2015",
	"status":  "1"
}
result, err := r.Table("order_json").Insert(data).RunWrite(session)
When i m tried to run this program i got the error as "missing operand" after "json_received":[ line.
Please help me to insert the data variable in rethink db via go programming..
答案1
得分: 2
Go语言不支持像你尝试做的那样的JSON字面量。
这是一个修复后的版本(在Play上查看)。
请注意,对于所有的子结构,在创建时必须声明类型。
你试图让json_received成为一个JSON对象列表,所以我使用了[]map[string]interface{}。
另外,正如其他人指出的,多行的map/list字面量在每行后面必须有一个逗号,比如:orderDesc,status。
package main
import "fmt"
func main() {
    var data = map[string]interface{}{
        "json_received": []map[string]interface{}{
            {
                "ezpOrderId": "ezp_123",
                "firstName":  "Vasanth",
                "lastName":   "K",
                "orderDesc":  "Sample",
            },
        },
        "created_on": "03-22-2015",
        "status":     "1",
    }
    fmt.Printf("%#v\n", data)
    //result, err := r.Table("order_json").Insert(data).RunWrite(session)
}
英文:
Go does not support json literals like you are trying to do.
Here's a fixed version (on Play).
Notice that for all sub structures, you must declare the type when creating it.
You were trying to make json_recieved to be a list of json objects, so I used []map[string]interface{}.
And, as others have pointed out, multi-line map/list literals must have a comma after each line, as in: orderDesc, status.
package main
import "fmt"
func main() {
	var data = map[string]interface{}{
		"json_received": []map[string]interface{}{
			{
				"ezpOrderId": "ezp_123",
				"firstName":  "Vasanth",
				"lastName":   "K",
				"orderDesc":  "Sample",
			},
		},
		"created_on": "03-22-2015",
		"status":     "1",
	}
	fmt.Printf("%#v\n", data)
	//result, err := r.Table("order_json").Insert(data).RunWrite(session)
}
答案2
得分: 0
你在json_received数组后面漏掉了逗号。
"json_received": [
    {
        "ezpOrderId": "ezp_123",
        "firstName": "Vasanth",
        "lastName": "K",
        "orderDesc": "Sample"
    }
], // <--
英文:
you are missing comma after json_received array
"json_received": [
        {
        "ezpOrderId":  "ezp_123",
        "firstName":  "Vasanth",
        "lastName":  "K",
        "orderDesc":  "Sample"
        }
    ] , //<--
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论