简体   繁体   中英

JSON.parse returning error

So this is my JSON.stringify'd return before I try to run JSON.parse

{"id":"2","name":"<small>L</small>(+)-Amethopterin Hydrate","class":"6.1","subclass":"","packing_group":"III","un":"2811","cas":"133073-73-1","poisons":"","hazardous":"Yes","restricted":"No","epa":"","added_by":"0","carcinogen":null},
{"id":"3","name":"(+)-Biotin 4-Nitrophenyl ester","class":"","subclass":"","packing_group":"","un":"","cas":"33755-53-2","poisons":"","hazardous":"No","restricted":"No","epa":"","added_by":"0","carcinogen":null},
{"id":"4","name":"(+)-Biotin N-hydroxysuccinimide ester","class":"","subclass":"","packing_group":"","un":"","cas":"35013-72-0","poisons":"","hazardous":"No","restricted":"No","epa":"","added_by":"0","carcinogen":null}

When I try to JSON.parse I get Unexpected end of JSON input . And I can't access it as a JSON object because it will say can't define id or something to that extent.



JSON.parse(this.searchService.searchJson(this.php_url));

this.searchService.searchJson(this.php_url) is basically what my JSON string is. Gives error as mentioned above.



Also if I just try to stringify 1 of the 3 elements, it'll give me Unexpected token u in JSON at position 0



Calling function:

searchJson(url: any): any 
{   
    let items: any = []; 
    let new_data: any = []; 
    $.getJSON(url , 
        function(data)
        {   
            let temp_items: any = {}; 
            console.log(data);
            $.each(data, function (key, val)
            {   
                new_data.push(JSON.stringify(val));
            }); 
        }); 
    return new_data;
} 

You have to wrap it with [] because that is an array of objects:

const data = [{"id":"2","name":"<small>L</small>(+)-Amethopterin Hydrate","class":"6.1","subclass":"","packing_group":"III","un":"2811","cas":"133073-73-1","poisons":"","hazardous":"Yes","restricted":"No","epa":"","added_by":"0","carcinogen":null}, {"id":"3","name":"(+)-Biotin 4-Nitrophenyl ester","class":"","subclass":"","packing_group":"","un":"","cas":"33755-53-2","poisons":"","hazardous":"No","restricted":"No","epa":"","added_by":"0","carcinogen":null}, {"id":"4","name":"(+)-Biotin N-hydroxysuccinimide ester","class":"","subclass":"","packing_group":"","un":"","cas":"35013-72-0","poisons":"","hazardous":"No","restricted":"No","epa":"","added_by":"0","carcinogen":null}]

console.log(data); will return [Object, Object, Object]

or if you wanna process as a JSON string you should do this:

const data = '[{"id":"2","name":"<small>L</small>(+)-Amethopterin Hydrate","class":"6.1","subclass":"","packing_group":"III","un":"2811","cas":"133073-73-1","poisons":"","hazardous":"Yes","restricted":"No","epa":"","added_by":"0","carcinogen":null}, {"id":"3","name":"(+)-Biotin 4-Nitrophenyl ester","class":"","subclass":"","packing_group":"","un":"","cas":"33755-53-2","poisons":"","hazardous":"No","restricted":"No","epa":"","added_by":"0","carcinogen":null}, {"id":"4","name":"(+)-Biotin N-hydroxysuccinimide ester","class":"","subclass":"","packing_group":"","un":"","cas":"35013-72-0","poisons":"","hazardous":"No","restricted":"No","epa":"","added_by":"0","carcinogen":null}]'

JSON.parse(data) will return too [Object, Object, Object]

Changed the calling function to this:

searchAjax(url: any): any
{   
    let new_data: any;
    return $.ajax({
        url: url,
        type: 'post',
        dataType: "json", 
        async: false

    }).responseText;
} 

The most likely cause was that my variable was that my variable was null at the time of being called because of async.

file Nmae: self.json
    [
          {
            "id": "2",
            "name": "<small>L</small>(+)-Amethopterin Hydrate",
            "class": "6.1",
            "subclass": "",
            "packing_group": "III",
            "un": "2811",
            "cas": "133073-73-1",
            "poisons": "",
            "hazardous": "Yes",
            "restricted": "No",
            "epa": "",
            "added_by": "0",
            "carcinogen": null
          },
          {
            "id": "3",
            "name": "(+)-Biotin 4-Nitrophenyl ester",
            "class": "",
            "subclass": "",
            "packing_group": "",
            "un": "",
            "cas": "33755-53-2",
            "poisons": "",
            "hazardous": "No",
            "restricted": "No",
            "epa": "",
            "added_by": "0",
            "carcinogen": null
          },
          {
            "id": "4",
            "name": "(+)-Biotin N-hydroxysuccinimide ester",
            "class": "",
            "subclass": "",
            "packing_group": "",
            "un": "",
            "cas": "35013-72-0",
            "poisons": "",
            "hazardous": "No",
            "restricted": "No",
            "epa": "",
            "added_by": "0",
            "carcinogen": null
          }
        ]
     $(document).ready(function($) {
            $.ajax({
                url: 'self.json',
                type: 'GET',
                dataType: 'json',
            })
            .done(function(respose) {
                for (var i = 0; i < respose.length; i++) {
                    resText = respose[i].id+' '+respose[i].name+' '+ respose[i].class+' '+respose[i].subclass;
                    console.log(resText);
                };
            })
            .fail(function() {
                console.log("error");
            })
            .always(function() {
                console.log("complete");
            });

        });

Output: 在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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