简体   繁体   English

JSON解析功能未解析

[英]JSON parse functions are not parsing

I am running a Javascript function on the latest version of Mozilla which receives a string which I want to convert to a JSON object. 我正在最新版本的Mozilla上运行Javascript函数,该函数接收到一个我想转换为JSON对象的字符串。 The conversion seems to be failing. 转换似乎失败。

The string is being generated on the server side in a Java function: 该字符串是在Java函数的服务器端生成的:

         result = "[{ \"userID\": 1 \"firstName\":\"John\" \"lastName\":\"Sheridan\" }{ \"userID\": 2 \"firstName\":\"Michael\" \"lastName\":\"Geribaldi\" }]";

(note that I am attempting to return an array of values for a list). (请注意,我正在尝试为列表返回值数组)。

The code on the client side is the ajax callback shown below: 客户端上的代码是ajax回调,如下所示:

var successFunc = function(data, textStatus, jqXHR)
{
    alert("Data: "+data);

    var obj = $.parseJSON(data);
    alert("Object: "+obj);
}

Apparently, the data is coming back to the callback and is being displayed in its string form, but the JSON parser is failing because the second alert is failing to appear. 显然,数据将返回到回调并以其字符串形式显示,但是JSON解析器失败,因为第二个警报未能显示。 I am sure something is wrong with my string but am having trouble figuring out what. 我确定我的琴弦出了点问题,但是很难弄清楚是什么。 The debugger is not telling me anything, I am just seeing a silent failure. 调试器没有告诉我任何信息,我只是看到了一个静默失败。

I have also attempted this using the JSON.parser() function. 我也尝试使用JSON.parser()函数进行此操作。 I am seeing the same thing. 我看到的是同一件事。 I am making a mistake somewhere. 我在某个地方犯了一个错误。 Can someone tell me where? 有人可以告诉我在哪里吗?

Your json is not valid, you are missing comma 您的json无效,您缺少逗号

In order to parse your json should be like this 为了解析你的json应该是这样的

[
 { "userID": 1, "firstName":"John", "lastName":"Sheridan" },
 { "userID": 2, "firstName":"Michael", "lastName":"Geribaldi" }
]

JSON is a format where data is in key:value pairs separeted by , and key and value are enclosed in double quotes , where objects are enclosed in {} braces and array is enclosed in [] hope you have got the your mistake that where your json is lacking. JSON是一种格式,其中数据位于key:value对之间,用分隔,并且key和value用double quotes ,将对象用{}括起来,将数组用[]希望您遇到错误,即缺少json。

  "[{ 
    \"userID\": 1 ,
    \"firstName\":\"John\",
    \"lastName\":\"Sheridan\",
     },
    { 
    \"userID\": 2 ,
    \"firstName\":\"Michael\",
    \"lastName\":\"Geribaldi\" }]"

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

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