如何使用jquery解析句子json?

时间:2018-12-18 21:17:37

标签: jquery json serialization

如何使用jquery解析此json? 需要您的帮助,在此先感谢!

我已经使用以下代码序列化了json,但是输出无法正确呈现。拆分函数var nameParts = this.name.split(']');

Json文件

 "Key Value Pair":  {

            "ORGANIZATION_INFO": [
                "Org Sentence 1",
                "Org Sentence 2",
                "Org Sentence 3"
            ],

            "EDUCATIONAL_QUALIFICATION": [
                "Edu Sentence 1",
                "Edu Sentence 2",
                "Edu Sentence 3"
            ]

    }

--------------------------------------------

序列化代码

function serializeFormObject(form)
{
    function trim(str)
    {
        return str.replace(/^\s+|\s+$/g,"");
    }

    debugger;

    var o = {};
    var a = $(form).serializeArray();
    $.each(a, function() {
        var nameParts = this.name.split(']');
        if (nameParts.length == 1) {
            // New value is not an array - so we simply add the new
            // value to the result object
            if (o[this.name] !== undefined) {
                if (!o[this.name].push) {
                    o[this.name] = [o[this.name]];
                }
                o[this.name].push(this.value || '');
            } else {
                o[this.name] = this.value || '';
            }
        }
        else {
            // New value is an array - we need to merge it into the
            // existing result object
            $.each(nameParts, function (index) {
                nameParts[index] = this.replace(/\]$/, '');
            });

            // This $.each merges the new value in, part by part
            var arrItem = this;
            var temp = o;
            $.each(nameParts, function (index) {
                var next;
                var nextNamePart;
                if (index >= nameParts.length - 1)
                    next = arrItem.value || '';
                else {
                    nextNamePart = nameParts[index + 1];
                    if (trim(this) != '' && temp[this] !== undefined)
                        next = temp[this];
                    else {
                        if (trim(nextNamePart) == '')
                            next = [];
                        else
                            next = {};
                    }
                }

                if (trim(this) == '') {
                    temp.push(next);
                } else
                    temp[this] = next;

                temp = next;
            });
        }
    });
    return o;
}

表单提交代码

$(function() {

    $('form').submit(function(e) {
      e.preventDefault();
       $('#result').text(JSON.stringify(serializeFormObject($('form'))));
      // $('#results').text(JSON.stringify(serializeFormObject($('form'))));
      // $('#update').text(JSON.stringify(serializeFormObject($('.edited-output'))));
      // $('#updates').text(JSON.stringify(serializeFormObject($('.edited-output'))));

      var jsons = $("#result").text();
      JSON.parse(jsons);
     $('#result').jsonFrill();
     $('#update').jsonFrill();
        return false;

    });
});

结果未正确显示,请检查下图

enter image description here

0 个答案:

没有答案