Вопрос

I keep getting the error: Uncaught SyntaxError: Unexpected token '

this is the full JSON response:

'app': {type: 'app', desc: 'Application'}, 'iso': {type: 'iso', desc: 'ISO Disk Image'}, 'fla': {type: 'fla', desc: 'Adobe Flash Document'}, 'dll': {type: 'dll', desc: 'Dynamic Link Library'}

I've tried putting brackets around the full response too, but that doesn't fix it. Am I doing JSON wrong? I've tried removing the parentheses but then it complains about characters. I'm trying to turn this response into a JavaScript Object, but it just doesn't want to do it!

Это было полезно?

Решение

A couple of problems there:

  1. In JSON, keys must be in double quotes (so must strings). Single quotes aren't allowed, and quotes are required.

  2. In JSON, the top level must always be an object or an array. You've quoted a series of property initializers, which must be inside an object.

Here's the valid version of that:

{
  "app": {"type": "app", "desc": "Application"},
  "iso": {"type": "iso", "desc": "ISO Disk Image"},
  "fla": {"type": "fla", "desc": "Adobe Flash Document"},
  "dll": {"type": "dll", "desc": "Dynamic Link Library"}
}

Changes:

  • Change all single quotes to double quotes.
  • Put double quotes around the keys (type, desc) that didn't have them.
  • Put the whole thing in {} so the top level is an object.

Другие советы

Here is your valid JSON String

{
    "app": {
        "type": "app",
        "desc": "Application"
    },
    "iso": {
        "type": "iso",
        "desc": "ISODiskImage"
    },
    "fla": {
        "type": "fla",
        "desc": "AdobeFlashDocument"
    },
    "dll": {
        "type": "dll",
        "desc": "DynamicLinkLibrary"
    }
}

You can always check for validity on http://jsonlint.com/

If you want to look for some valid JSON formats look here

I've just add double quotes everywhere, put {} around, and it works :

var objet = '{"app": {"type": "app", "desc": "Application"},"iso": {"type": "iso", "desc": "ISO Disk Image"},"fla": {"type":"fla", "desc": "Adobe Flash Document"},"dll": {"type": "dll", "desc": "Dynamic Link Library"}}';
console.log($.parseJSON(objet));

You can retrieve my test here

See json.org for the full JSON standard.

You'll see that there are two problems here:

JSON data is represented as an object ( {} ) or an array ( [] ). Therefore you need to wrap everything within one or the other, depending on what is appropriate for your data (likely object, from the looks of your data).

You also need to use double-quotation marks on your strings. From json.org:

A string is a sequence of zero or more Unicode characters, wrapped in double quotes, using backslash escapes

Finally, you can use JSON Lint to catch these sorts of problems quickly and easily.

Here's your data in valid JSON:

{
    "app": {
        "type": "app",
        "desc": "Application"
    },
    "iso": {
        "type": "iso",
        "desc": "ISODiskImage"
    },
    "fla": {
        "type": "fla",
        "desc": "AdobeFlashDocument"
    },
    "dll": {
        "type": "dll",
        "desc": "DynamicLinkLibrary"
    }
}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top