簡體   English   中英

合並兩個有效的json對象時出現意外的令牌錯誤

[英]Unexpected token error when combining two valid json object

我正在做一個Web應用程序,當我嘗試解析該文件時,我需要從服務器加載JSON文件,我遇到了Unexpected Token錯誤。 然后我發現當一個文件中出現兩項時會發生問題

這是兩個JSON項目:

{
    "黃南":{"id":10973,"name":"黃南","prov":"青海","latt":35.519549,"logi":102.015248}, 
    "海北":{"id":10970,"name":"海北","prov":"青海","latt":36.954413,"logi":100.900998}
}

我曾嘗試JSON.parse鉻控制台, json.loads Python和JSONlint.com ,他們都引發錯誤。

有趣的部分是,當我嘗試分別加載它們時,沒有錯誤,但是只要將它們一起加載,就會拋出錯誤

那么誰能告訴我發生了什么以及如何避免這種情況? 謝謝大家,如果我的描述中有任何語法問題,我們深表歉意。

這是沒有更多信息的猜測,但是我猜測python 2.x沒有設置編碼,因為除此之外,沒有其他原因。

嘗試了3.4和2.7中可用的最基本的操作。 它在3.4中沒有問題。 在2.7版中,您需要使用utf-8,否則會出現錯誤。

這將適用於python 3.x,但在2.x中失敗

#!/usr/bin/env python3.4
import json
j = """{
    "黃南":{"id":10973,"name":"黃南","prov":"青海","latt":35.519549,"logi":102.015248},
    "海北":{"id":10970,"name":"海北","prov":"青海","latt":36.954413,"logi":100.900998}
}"""

o = json.loads(j)
print(json.dumps(o,indent=1))

這將在2.7或2.6中運行

#!/usr/bin/env python2.7
# -*- coding: utf-8 -*-
import json
j = """{
    "黃南":{"id":10973,"name":"黃南","prov":"青海","latt":35.519549,"logi":102.015248},
    "海北":{"id":10970,"name":"海北","prov":"青海","latt":36.954413,"logi":100.900998}
}"""

o = json.loads(j)
print(json.dumps(o,indent=1))

意外標記 '&lt;', "<div id="text_translate"><p> 我是 Reactjs 的初學者和 StackOverflow 的新手。 實際上,我正在嘗試將數據從后端傳遞到前端。 但不是從后端 url 獲取 JSON 數據,而是從前端的 index.html 獲取數據。 我的后端是nodejs。 基本上,我想從后端獲取 JSON 數據並將數據發布到前端的控制台中。 但我得到這個SyntaxError: Unexpected token '&lt;', "&lt;.DOCTYPE "... is not valid JSON誰能幫我解決這個問題。 <a href="/questions/tagged/reactjs" class="post-tag" title="顯示標記為“reactjs”的問題" aria-label="show questions tagged 'reactjs'" rel="nofollow noreferrer" aria-labelledby="reactjs-container">reactjs</a> <a href="/questions/tagged/nodejs" class="post-tag" title="顯示標記為“nodejs”的問題" aria-label="show questions tagged 'nodejs'" rel="nofollow noreferrer" aria-labelledby="nodejs-container">nodejs</a></p><p> <strong>前端代碼</strong></p><pre>App.js import React from 'react'; import {useState, useEffect} from 'react'; import {getOrder} from './ApiCalls.js' function App() { const[values, setValues]=useState({ amount:0, orderId:'' }) const{amount, orderId}=values useEffect(() =&gt; { createorder() }, []) const createorder=()=&gt;{ getOrder().then(response=&gt;console.log(response)) } const showRazorPay=()=&gt;{ const form=document.createElement('form') form.setAttribute('action',`${process.env.REACT_APP_BACKEND}/payment/callback`); form.setAttribute('method',"POST"); const script=document.createElement("script"); script.src= "https://checkout.razorpay.com/v1/checkout.js"; script.setAttribute("data-key",process.env.REACT_APP_DATA_KEY); script.setAttribute("data-amount", amount); script.setAttribute("data-prefill.contact","9561315545"); script.setAttribute("data-order_id", orderId); script.setAttribute("data-prefill.name", "Priyanka Chaudhari"); script.setAttribute("data-image", `${process.env.REACT_APP_BACKEND}/logo`) script.setAttribute("data-buttontext","Donate Now;"). document.body;appendChild(form). form;appendChild(script). const input= document;createElement("input"). input;type="hidden". input;custom="Hidden Element"; } return ( &lt;div&gt; &lt;/div&gt; ); } export default App;</pre><pre> ApiCalls.js export const getOrder=()=&gt;{ return fetch(`${process.env.REACT_APP_BACKEND}/createorder`,{ method: "GET", headers: { 'Content-Type':'application/json' } }).then(response=&gt;response.json()).catch((err)=&gt;console.log(err)) }</pre><p> <strong>后端代碼</strong></p><pre>App.js const express=require('express') const bodyParser=require('body-parser') const cors=require('cors') const app=express() const PaymentRoute=require('./PaymentRoute') app.use(bodyParser.json()) app.use(cors()) app.use('/api',PaymentRoute); app.listen(5000,()=&gt;{ console.log(`App is running at 5000 port`) })</pre><pre> PaymentRoute.js const express=require('express') const router=express.Router() const{CreateOrder,paymentCallback,getLogo}=require('./PaymentController') router.get('/createorder',CreateOrder); router.post('/payment/callback',paymentCallback) router.get('/logo',getLogo) module.exports=router;</pre><pre> PaymentController.js require('dotenv').config() const Razorpay=require('razorpay') const uniqueId=require('uniqid') const path=require('path') var instance = new Razorpay({ key_id: process.env.KEY_ID, key_secret: process.env.SECRET_KEY }) // instance.payments.fetch(paymentId) exports.CreateOrder=(req,res)=&gt;{ var options = { amount: 50000, // amount in the smallest currency unit currency: "INR", receipt: uniqueId() }; instance.orders.create(options, function(err, order) { if(err){ return res.status(500).json({ error:err }) } res.json(order) }); } exports.paymentCallback=(req,res)=&gt;{ } exports.getLogo=(req,res)=&gt;{ res.sendFile(path.join(__dirname,'donate-image.png')) }</pre></div>

[英]Unexpected token '<', "<!DOCTYPE "... is not valid JSON

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

相關問題 未捕獲的語法錯誤意外的令牌:,但是服務器從服務器接收到有效的json對象 意外標記 '&lt;', "<div id="text_translate"><p> 我是 Reactjs 的初學者和 StackOverflow 的新手。 實際上,我正在嘗試將數據從后端傳遞到前端。 但不是從后端 url 獲取 JSON 數據,而是從前端的 index.html 獲取數據。 我的后端是nodejs。 基本上,我想從后端獲取 JSON 數據並將數據發布到前端的控制台中。 但我得到這個SyntaxError: Unexpected token '&lt;', "&lt;.DOCTYPE "... is not valid JSON誰能幫我解決這個問題。 <a href="/questions/tagged/reactjs" class="post-tag" title="顯示標記為“reactjs”的問題" aria-label="show questions tagged 'reactjs'" rel="nofollow noreferrer" aria-labelledby="reactjs-container">reactjs</a> <a href="/questions/tagged/nodejs" class="post-tag" title="顯示標記為“nodejs”的問題" aria-label="show questions tagged 'nodejs'" rel="nofollow noreferrer" aria-labelledby="nodejs-container">nodejs</a></p><p> <strong>前端代碼</strong></p><pre>App.js import React from 'react'; import {useState, useEffect} from 'react'; import {getOrder} from './ApiCalls.js' function App() { const[values, setValues]=useState({ amount:0, orderId:'' }) const{amount, orderId}=values useEffect(() =&gt; { createorder() }, []) const createorder=()=&gt;{ getOrder().then(response=&gt;console.log(response)) } const showRazorPay=()=&gt;{ const form=document.createElement('form') form.setAttribute('action',`${process.env.REACT_APP_BACKEND}/payment/callback`); form.setAttribute('method',"POST"); const script=document.createElement("script"); script.src= "https://checkout.razorpay.com/v1/checkout.js"; script.setAttribute("data-key",process.env.REACT_APP_DATA_KEY); script.setAttribute("data-amount", amount); script.setAttribute("data-prefill.contact","9561315545"); script.setAttribute("data-order_id", orderId); script.setAttribute("data-prefill.name", "Priyanka Chaudhari"); script.setAttribute("data-image", `${process.env.REACT_APP_BACKEND}/logo`) script.setAttribute("data-buttontext","Donate Now;"). document.body;appendChild(form). form;appendChild(script). const input= document;createElement("input"). input;type="hidden". input;custom="Hidden Element"; } return ( &lt;div&gt; &lt;/div&gt; ); } export default App;</pre><pre> ApiCalls.js export const getOrder=()=&gt;{ return fetch(`${process.env.REACT_APP_BACKEND}/createorder`,{ method: "GET", headers: { 'Content-Type':'application/json' } }).then(response=&gt;response.json()).catch((err)=&gt;console.log(err)) }</pre><p> <strong>后端代碼</strong></p><pre>App.js const express=require('express') const bodyParser=require('body-parser') const cors=require('cors') const app=express() const PaymentRoute=require('./PaymentRoute') app.use(bodyParser.json()) app.use(cors()) app.use('/api',PaymentRoute); app.listen(5000,()=&gt;{ console.log(`App is running at 5000 port`) })</pre><pre> PaymentRoute.js const express=require('express') const router=express.Router() const{CreateOrder,paymentCallback,getLogo}=require('./PaymentController') router.get('/createorder',CreateOrder); router.post('/payment/callback',paymentCallback) router.get('/logo',getLogo) module.exports=router;</pre><pre> PaymentController.js require('dotenv').config() const Razorpay=require('razorpay') const uniqueId=require('uniqid') const path=require('path') var instance = new Razorpay({ key_id: process.env.KEY_ID, key_secret: process.env.SECRET_KEY }) // instance.payments.fetch(paymentId) exports.CreateOrder=(req,res)=&gt;{ var options = { amount: 50000, // amount in the smallest currency unit currency: "INR", receipt: uniqueId() }; instance.orders.create(options, function(err, order) { if(err){ return res.status(500).json({ error:err }) } res.json(order) }); } exports.paymentCallback=(req,res)=&gt;{ } exports.getLogo=(req,res)=&gt;{ res.sendFile(path.join(__dirname,'donate-image.png')) }</pre></div> 解析有效的JSON時出現&#39;Uncaught SyntaxError:Unexpected token:&#39; 從asyncStorage獲取時的JSON.parse(object)引發錯誤:位置1的JSON中的意外令牌o 當您在json中有兩行時,如何解決意外的令牌錯誤? 嘗試在簡單對象上執行JSON.parse時出現“意外令牌”錯誤 未捕獲的SyntaxError:意外的令牌:對於有效的JSON 使用Javascript將字符串轉換為對象(錯誤:JSON中位置1處的意外令牌t) 使用JSON.parse將字符串(變量)轉換為對象,錯誤意外令牌 SyntaxError意外令牌{在JavaScript中使用json對象時
 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM