使用JavaScript将字符串转换为JSON

时间:2018-07-09 21:13:29

标签: javascript json sqlite

我有一个def select_image(): # grab a reference to the image panels global panelA, panelB # open a file chooser dialog and allow the user to select an input # image path = tkinter.filedialog.askopenfilename() # ensure a file path was selected if len(path) > 0: # load the image from disk, convert it to grayscale, and detect # edges in it image = cv2.imread(path) edged = stacker(os.path.dirname(os.path.dirname(path))) # OpenCV represents images in BGR order; however PIL represents # images in RGB order, so we need to swap the channels image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) # convert the images to PIL format... image = Image.fromarray(image) edged = Image.fromarray(edged) # ...and then to ImageTk format image = ImageTk.PhotoImage(image) edged = ImageTk.PhotoImage(edged) # if the panels are None, initialize them if panelA is None or panelB is None: # the first panel will store our original image panelA = tk.Label(image=image) panelA.image = image panelA.pack(side="left", padx=10, pady=10) # while the second panel will store the edge map panelB = tk.Label(image=edged) panelB.image = edged panelB.pack(side="right", padx=10, pady=10) # otherwise, update the image panels else: # update the pannels panelA.configure(image=image) panelB.configure(image=edged) panelA.image = image panelB.image = edged # initialize the window toolkit along with the two image panels root = tk.Tk() panelA = None panelB = None print("done") # create a button, then when pressed, will trigger a file chooser # dialog and allow the user to select an input image; then add the # button the GUI btn = tk.Button(root, text="Select an image", command=select_image) print("done1") btn.pack(side="bottom", fill="both", expand="yes", padx="10", pady="10") print("done2") # kick off the GUI root.mainloop() print("done3") ,看起来像这样:

string

您会注意到,它是JSON格式。我将其存储在"{""c1"": ""value1"", ""c2"": ""value2""}" 数据库中,然后使用以下Javascript代码以SQLITE格式再次获取它:

JSON

但是我得到这个错误:

  var req= "SELECT json_column from my_table";
  var result = execSQL(req);
  for (var index in res) {
    var row= res[index];
    consoleLog("test getad ", JSON.parse(row["json_column "]));

您能告诉我为什么会出现此错误吗?我花了数小时试图解决它,但没有成功。我可以根据需要更改<JSContext: 0x1c0044aa0> SyntaxError: JSON Parse error: Expected '}' 格式,我所需要的只是从string作为SQLITE对象获取它。 预先谢谢你。

2 个答案:

答案 0 :(得分:1)

您的字符串不是有效的JSON,该字符串基于您的字符串内容。

AVPlayerItem

如您所见,键/值对被一个双引号'{"c1": "value1", "c2": "value2"}' 包围,而不是两个,并且整个字符串都被单引号"包围。如果整个字符串都用双引号引起来,则需要对内部的字符串进行转义,像这样

'

为进一步了解JSON,这篇文章有很多内容

以下是显示其输出的示例

"{\"c1\": \"value1\", \"c2\": \"value2\"}"

答案 1 :(得分:-1)

您的字符串格式不正确。这就是为什么JSON.parse()无法读取它的原因。

您的字符串:

"{""c1"": ""value1"", ""c2"": ""value2""}"

尝试这样:

'{"c1": "value1", "c2": "value2"}'