简体   繁体   English

Razor Page & jQuery 中位置 1 处的 JSON 中的意外标记 o

[英]Unexpected token o in JSON at position 1 in Razor Page & jQuery

I have a JSON object as below and I need to pass some of the object via a jQuery Attribute tag.我有一个 JSON 对象,如下所示,我需要通过 jQuery 属性标签传递一些对象。 I am getting a JSON parse error but I am not sure why.我收到一个 JSON 解析错误,但我不知道为什么。

  "Walkers" : "true",
  "Owners" :[
  {
        "Name":"Bob",
        "Description":"Old",
        "Pets": [
          {"Name" : "Cuddles", "Age": "8"}, 
          {"Name" : "Pounce", "Age": "3"}
        ]
      },
      {
        "Name":"Mary",
        "Description":"Older",
        "Pets": [
          {"Name" : "Red", "Age": "13"}, 
          {"Name" : "Leaf", "Age": "1"}
        ]
      }
      ]

In my razor page I am serialising the section of the JSON object I need.在我的剃刀页面中,我正在序列化我需要的 JSON 对象部分。

@foreach (var people in myjson) {

<p>@people.Walkers</p> //true

<div id="mytarget" data-owners='@Json.Serialize(people.Owners)'> </div>

}

In jQuery:在 jQuery 中:

var val = $("#mytarget").data('owners');

            console.log("json " + val); // result: json [object Object],[object Object]

            console.log("parsed " + JSON.parse(val)); // result: VM7:1 Uncaught SyntaxError: Unexpected token o in JSON at position 1 at JSON.parse (<anonymous>)

If I don't use JSON.Parse and just try and loop through the object in JQuery I just end up with '0' and '1'如果我不使用 JSON.Parse 而只是尝试遍历 JQuery 中的对象,我只会得到“0”和“1”

var val = $("#mytarget").data('owners');

for (var obj in val) {
   Console.log(obj);
}

Result '0' and '1'.

I have a JSON object as below and I need to pass some of the object via a jQuery Attribute tag.我有一个 JSON 对象,如下所示,我需要通过 jQuery 属性标签传递一些对象。 I am getting a JSON parse error我收到 JSON 解析错误

If you use console.log(val);如果你使用console.log(val); to output the val to console tab, you would find you have gotten a json array, like below.要将val输出到控制台选项卡,您会发现您获得了一个 json 数组,如下所示。

在此处输入图片说明

when I try to use obj.Name I get 'undefined'当我尝试使用 obj.Name 时,我得到“未定义”

To extract data of owner and pets from that array, you can refer to the following code snippet.要从该数组中提取所有者宠物的数据,您可以参考以下代码片段。

var val = $("#mytarget").data('owners');

//console.log("json " + val);

console.log(val);


$.each(val, function (index, owner) {
    //owner object
    console.log("Name: "+owner.name+"; Description: "+owner.description);

    //pets of current owner
    $.each(owner.pets, function (index, pet) {
            console.log("Name: "+pet.name+"; Age: "+pet.age);
    })
})  

在此处输入图片说明

That's because you're passing an array of object instead of an object so in your console you're getting something like this:那是因为你传递的是一个对象数组而不是一个对象,所以在你的控制台中你会得到这样的东西:

 [ { "Name":"Bob", "Description":"Old", "Pets": [ {"Name" : "Cuddles", "Age": "8"}, {"Name" : "Pounce", "Age": "3"} ] }, { "Name":"Mary", "Description":"Older", "Pets": [ {"Name" : "Red", "Age": "13"}, {"Name" : "Leaf", "Age": "1"} ] } ]

You can't parse an array like a object will be return a error.你不能像一个对象那样解析一个数组会返回一个错误。

You can also use devtools to see how the data is coming , maybe it is coming in another format, here is the docs for more info https://developers.google.com/web/tools/chrome-devtools/inspect-styles/您还可以使用devtools 查看数据的来源,也许它是另一种格式,这里是更多信息的文档https://developers.google.com/web/tools/chrome-devtools/inspect-styles/

The error is in the for bucle the correct syntax is:错误在 for bucle 中,正确的语法是:

var val = $("#mytarget").data('owners');

for (i = 0; i < val.length; i++) {
   console.log(val[i])
}

here is the docs: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Loops_and_iteration这是文档: https : //developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Loops_and_iteration

Or you can use map: https://developer.mozilla.org/es/docs/Web/JavaScript/Referencia/Objetos_globales/Array/map或者你可以使用地图: https : //developer.mozilla.org/es/docs/Web/JavaScript/Referencia/Objetos_globales/Array/map

var val = $("#mytarget").data('owners');

val.map(item => {
  console.log(item)
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM