Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Issue with JSON parsing

I am a complete noob with JSON parsing in jQuery. Thought I got the response... My data is in this form:

Array(
[1]=>abc,
[3]=>mango,
[4]=>apple,
[5]=>fruits
)

In this way I want this list to appear as an autocomplete list. I am using.

    jQuery("#name").autocomplete( '<?php echo HTTP_PATH.'/songs/sss'; ?>', {
    multiple: true,
    mustMatch: true,        
    matchContains: true,
    autoFill: false,
    dataType: "json",
    parse: function(data) {
            return jQuery.map(data, function(item) {
                return { data: item, value: item.label, result: item.label};
            });
        },
        formatItem: function(item) {
            return item.label;
        },
        formatResult: function(item) {
            return item.id;
        },
        formatMatch: function(item) {
            return item.label;
        }

});

I want the value when it shows list, i.e. the label from my data. When I select a label then it should show the label. But at the time of submission it should actually submit the key. I mean I want it to work as select box of HTML.

Returned JSON

 [{"id":1,"label":"Mehdi Hassan"},{"id":2,"label":"Jagjit Singh"},{"id":3,"label":"Suresh Vadekar"}]
like image 474
Rahul Singh Avatar asked Apr 23 '12 19:04

Rahul Singh


People also ask

Why does JSON parse not work?

Null values and empty strings (“”) are valid JSON values, but the state of being undefined is not valid within JSON. JSON. parse() will fail if it encounters an undefined value.

What is JSON parsing error?

parse errors are a subset of SyntaxError error type. Debugging console throws around 32 different errors messages when you mess up your JSON data. And some of them are very tricky to debug; and yes I am talking about you unexpected non-whitespace character after JSON data .

How do you handle JSON parsing errors?

The most common way to handle JSON parse error is using try-catch block. If the JSON string is valid, it will return a JavaScript object. If the JSON string is invalid, it will throw a SyntaxError.

What is the weakness of JSON?

JSON isn't as robust a data structure as XML is. There is no ability to add comments or attribute tags to JSON, which limits your ability to annotate your data structures or add useful metadata. The lack of standardized schemas limits your ability to programmatically verify that your data is correct.


1 Answers

Your JSON seems to not be an array, just an object (hash map). Check out official docs:

Expected data format

The data from local data, a url or a callback can come in two variants:

  • An Array of Strings: [ "Choice1", "Choice2" ]

  • An Array of Objects with label and value properties: [ { label: "Choice1", value: "value1" }, ... ]

In your case it should be in the following format:

[
   {"1":"Shad.aab"},
   {"158":"Adadad"},
   {"159":"Asdadad"},
   {"166":"Abbas"},
   {"167":"Sdadad"},
   {"171":"Shadaab Please check it out"},
   {"173":"Check This Please"},
]

(Remember that left side is label, right value, I suppose in your data, all should be reversed...)

like image 162
IProblemFactory Avatar answered Oct 12 '22 06:10

IProblemFactory