如何将此字符串解析为json对象?

时间:2019-03-26 09:18:26

标签: javascript json

我有这个字符串

var str = '{"{\n  \"lightid\": \"ID1\",\n  \"brightness\": \"123\",\n  \"RGB\": \"1,2,3\"\n}":""}';

如何将其解析为JSON对象?

2 个答案:

答案 0 :(得分:0)

只需拨打电话

JSON.parse('{\n  \"lightid\": \"ID1\",\n  \"brightness\": \"123\",\n  \"RGB\": \"1,2,3\"\n}');

答案 1 :(得分:0)

您的Option Explicit Sub trst() With ThisWorkbook.Worksheets("Sheet1") '<- Refer to sheet you are coding. Change sheet name if needed VarNUMCB = InputBox("Insert User ID Number") If .Range("H3").Value >= 0 Then '<- Use "." before range to make sure that your are targeting the sheet used in the "With statement" .Range("J3").Value = .Range("J3").Value & " " & VarNUMCB '<- J3 value with a following by a space and the value VarNUMCB Else End If End With End Sub 字符串格式错误。当前是:

json

计算结果为

var str = '{"{\n  \"lightid\": \"ID1\",\n  \"brightness\": \"123\",\n  \"RGB\": \"1,2,3\"\n}":""}';

您需要使用:

{"{
   "lightid": "ID1",
   "brightness": "123",
   "RGB": "1,2,3"
}":""}

并使用默认的JSON解析器:

var str = '{\n  \"lightid\": \"ID1\",\n  \"brightness\": \"123\",\n  \"RGB\": \"1,2,3\"\n}'

似乎您将获得此类值的列表。在这种情况下, 构造将是:

var jsObj = JSON.parse(str);
console.log("jsonObj.brightness", jsonObj.brightness);