JSON解析问题 [英] Issue with JSON parsing

查看:88
本文介绍了JSON解析问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个在jQuery中使用JSON解析的完整菜鸟。以为我收到了回复......我的数据是这样的:

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;
        }

});

我希望显示列表时的值,即我的数据中的标签。当我选择标签时,它应该显示标签。但在提交时,它实际上应该提交密钥。
我的意思是我想让它作为HTML的选择框工作。

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.

返回JSON

 [{"id":1,"label":"Mehdi Hassan"},{"id":2,"label":"Jagjit Singh"},{"id":3,"label":"Suresh Vadekar"}]


推荐答案

你的JSON似乎不是一个数组,只是一个对象(哈希映射)。
查看官方文档

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

预期数据格式

来自本地数据,网址或回调的数据可以有两个
变体:

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


  • 字符串数组:[Choice1,Choice2]

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

具有标签和值属性的对象数组:[{label:Choice1,value:value1},
...]

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...)

这篇关于JSON解析问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆