2017-06-22 102 views
1

我想读取json字符串并输出html页面上的某些字段,并且出现此错误“SyntaxError:JSON.parse:JSON数据第1行第2列的意外字符“ 这是我发送的json。如何正确读取json字符串

{ 
    "Order0" : { 
    "user" : "Name", 
    "products" : [ { 
     "discription" : "freddo", 
     "productName" : "sketo", 
     "price" : 22.3, 
     "id" : 1 
    }, { 
     "discription" : "frape", 
     "productName" : "glyko", 
     "price" : 22.3, 
     "id" : 1 
    }, { 
     "discription" : "cappouchino", 
     "productName" : "sketo", 
     "price" : 22.3, 
     "id" : 1 
    } ], 
    "id" : 0, 
    "date" : null, 
    "temp" : "Get" 
    }, 
    "Order1" : { 
    "user" : "Name", 
    "products" : [ { 
     "discription" : "freddo", 
     "productName" : "sketo", 
     "price" : 22.3, 
     "id" : 1 
    }, { 
     "discription" : "frape", 
     "productName" : "glyko", 
     "price" : 22.3, 
     "id" : 1 
    }, { 
     "discription" : "cappouchino", 
     "productName" : "sketo", 
     "price" : 22.3, 
     "id" : 1 
    } ], 
    "id" : 0, 
    "date" : null, 
    "temp" : "Get" 
    } 
} 

在此先感谢您。 [编辑]

<!DOCTYPE html> 
<!-- 
To change this license header, choose License Headers in Project Properties. 
To change this template file, choose Tools | Templates 
and open the template in the editor. 
--> 
<html> 
    <head> 
     <title>TODO supply a title</title> 
     <meta charset="UTF-8"> 
     <meta name="viewport" content="width=device-width, initial-scale=1.0"> 
     <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> 
     <script> 
      var nIntervId; 
      function fun() { 
       nIntervId = setInterval(proxy, 2 * 1000); 
      } 
      function proxy() { 
       $.getJSON('events', function(data) { 
       var obj = JSON.parse(data); 
       console.log(obj.toString()); 
       }); 
      } 
      function stop() { 
       clearInterval(nIntervId); 
      } 
     </script> 

    </head> 
    <body onload="fun();"> 
     <div id="data"></div> 
    </body> 
</html> 

这是将处理JSON [EDIT2] 与window.alert(JSON.stringify(data)); 代码我得到我想要的字符串,但我如何得到某些领域?

+0

有什么不对的JSON作为展示。你确定这是你的浏览器得到的JSON吗?尝试调试您的代码并确保。如果您需要帮助,则需要显示该代码。 –

+1

您分享的代码没有任何问题。在创建它和你试图解析它之间必须要有一些东西。您需要提供[mcve]来演示问题。 – Quentin

+0

你的对象没有任何问题。你在调用'JSON.parse(string)'还是'JSON.stringify(object)'? – vbguyny

回答

2

$.getJSON自动解析字符串,所以data已经是一个对象,并且再次调用parse就会抛出错误。

从jQuery的文档:

The success callback is passed the returned data, which is typically a JavaScript object or array as defined by the JSON structure and parsed using the $.parseJSON() method. It is also passed the text status of the response.

来源:http://api.jquery.com/jquery.getjson/

为了访问你可以使用点符号对象中的数据。看你上面此处使用的示例是一个例子:

var user = data.Order0.user; 
// user variable now holds the string "Name" 

您也可以用括号访问:

var user = data["Order0"]["user"]; 

要得到产品,您可以使用相同的语法,但由于产品是一个数组你需要通过索引或循环访问值来访问它。

指数:

var order0products = data.Order0.products; 
var product1 = order0products[0]; 
var price = product1.price; 
// The price variable now holds the value 22.3 

循环:

var order0products = data.Order0.products; 
var length = order0products.length; 
for (var index = 0; index < length; index++) { 
    console.log(order0products.price); 
} 
// You will see each products price for order0 in your console. 
+0

是的,作品非常感谢你,但我将如何获得产品? @ AtheistP3ace –

+0

@SinSor通过访问产品的示例更新了答案。 – AtheistP3ace

+0

是的,我得到的价格谢谢你解释得很好的答案! –