无法理解此JSON分析错误

时间:2019-05-28 15:14:21

标签: javascript json

解析JSON字符串时JSON解析错误

我在JS代码下面有这个,并且给出错误。我想打印engineCode和值吗?

谢谢。

//I have update the code. Sorry about the confusion ...
let a = '{ "car": "Honda", "specs": {"engineCode": 1001} }';
let b = JSON.parse(a);
console.log(b); //prints json
//JSON.parse(b.engineCode); //please ignore this .....
JSON.parse(b.specs); //error while parsing and how to fix??

"SyntaxError: Unexpected token u in JSON at position 0
    at JSON.parse (<anonymous>)
    at bajahisowi.js:7:6
    at https://static.jsbin.com/js/prod/runner-4.1.7.min.js:1:13924
    at https://static.jsbin.com/js/prod/runner-4.1.7.min.js:1:10866"

2 个答案:

答案 0 :(得分:1)

您无需再次解析,只需使用点符号即可使用

import bs4
import requests
from bs4 import BeautifulSoup


def parsePrice():
    r = requests.get('https://www.finanzen.net/realtimekurs/Apple')
    soup = BeautifulSoup(r.text,"lxml")
    price = soup.find_all('div',{'class':'price'})[0].find('span').text
    return price


while True:
    print("Price:"+str(parsePrice()))

答案 1 :(得分:0)

正如其他人已经说过的那样,无需重新解析。考虑到您的JSON,请尝试console.log(b.specs.engineCode)

{
  "car": "Honda",
  "specs": {
    "engineCode": 1001
  }
}

如果您想真的b.engineCode,请重构您的JSON以反映以下内容:

{
  "car": "Honda",
  "engineCode": 1001
}
相关问题