1

This is driving me slightly crazy.

I am proficient in getting a single Ajax request / autocomplete.

However, now I am trying get a multi-column result working. I am combining: Autocomplete Remote and Custom Display. from the JQuery autocomplete examples.

The json_encoded array returns a string looks like:

a)

[
    {"value":"myvalue1","label":"mylabel1","desc":"mydescription1"},
    {"value":"myvalue2","label":"mylabel2","desc":"mydescription2"}
]

The Autocomplete remote shows a different formatting. Should it be:

b)

[
    {value:"myvalue1",label:"mylabel1",desc:"mydescription1"},
    {value:"myvalue2",label:"mylabel2",desc:"mydescription2"}
]

Which is the correct format - a or b? I am hoping "a", because it requires less code.

Thanks!

1

3 Answers 3

1

I think that the correct version is the first one, A, the second is not a valid JSON as it doesn't pass the validation. you must put " before the keys/properties

For example this is the json that is returned from the jQuery demo:

[
    {
        "id": "Coccothraustes coccothraustes",
        "label": "Hawfinch",
        "value": "Hawfinch"
    },
    {
        "id": "Accipiter gentilis",
        "label": "Northern Goshawk",
        "value": "Northern Goshawk"
    },
    {
        "id": "Accipiter nisus",
        "label": "Eurasian Sparrow Hawk",
        "value": "Eurasian Sparrow Hawk"
    },
    {
        "id": "Surnia ulula",
        "label": "Northern Hawk Owl",
        "value": "Northern Hawk Owl"
    }
]
Sign up to request clarification or add additional context in comments.

1 Comment

I had forgotten all about jsonlint so thank you for the reminder. I will assume the autocomplete will work with this, based on your code example from JQ UI's website. I wonder why they used two different formats?? Thanks so much.
1

You should be putting quotes around your keys/properties

{"foo":"bar"} is recommended over {foo:"bar"}

http://ejohn.org/blog/the-state-of-json/

http://ejohn.org/blog/ecmascript-5-strict-mode-json-and-more/

2 Comments

This answer was also correct, but the link provided in the answer below were more definitive for me personally. If I could award 2 correct answers, I would.
Cool. What link did he provide?
-1

it should be B if you want the plugin does something like obj.value or obj.label

2 Comments

B is not a valid Json i think
I will be using .value so hopefully validated json will work ok too.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.