解析对象后,保持对象在JSON字符串中的顺序 [英] Keep order of objects inside a JSON String after they are parsed

查看:290
本文介绍了解析对象后,保持对象在JSON字符串中的顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从API函数接收以下JSON字符串.

I receive the following JSON string from an API function.

"Inbound": {
    "callRelatedFields": ["ANI",
    "DNIS"],
    "objects": {
        "Contact": [{
            "displayName": "Name",
            "apiName": "Name"
        },
        {
            "displayName": "Email",
            "apiName": "Email"
        }],
        "Account": [{
            "displayName": "Account Name",
            "apiName": "Name"
        },
        {
            "displayName": "Phone",
            "apiName": "Phone"
        },
        {
            "displayName": "Fax",
            "apiName": "Fax"
        }],
        "cnx__Phone__c": [{
            "displayName": "Phone Name",
            "apiName": "Name"
        },
        {
            "displayName": "Phone Number Line 1",
            "apiName": "cnx__Phone_Number_Line_1__c"
        },
        {
            "displayName": "Phone Number Line 2",
            "apiName": "cnx__Phone_Number_Line_2__c"
        },
        {
            "displayName": "Type",
            "apiName": "cnx__Type__c"
        },
        {
            "displayName": "Location",
            "apiName": "cnx__Location__c"
        },
        {
            "displayName": "Call Manager",
            "apiName": "cnx__Call_Manager__c"
        },
        {
            "displayName": "Mac Address",
            "apiName": "cnx__Mac_Address__c"
        }]
    },
    "screenPopSettings": {
        "screenPopsOpenWithin": "ExistingWindow",
        "SingleMatch": {
            "screenPopType": "PopToEntity"
        },
        "NoMatch": {
            "screenPopType": "DoNotPop"
        },
        "MultipleMatches": {
            "screenPopType": "DoNotPop"
        }
    }
}

"objects"中对象的顺序很重要! 但是当我用JSON.parse解析此JSON字符串时,这些对象的顺序就会丢失.

The order of the objects inside "objects" is important! But when i parse this JSON string with JSON.parse, the order of those objects is lost.

在解析这些对象之后,是否有什么好方法可以保持这些对象的顺序.

Is there any good way to keep the order of those objects after they are parsed.

我试图操纵字符串并将整个"objects"转换为数组,但是事实证明,这变得过于复杂和棘手.

I tried to manipulate the string and convert the whole "objects" into an array, but this turned out to become way too complicated and hacky.

推荐答案

我怀疑让您认为键已更改顺序的是Chrome devtools显示对象,其键按字母顺序排序.而如果您使用Object.keys()或等效的JS手动遍历密钥,则会发现它们按照JSON字符串中定义的顺序出现.

I have a suspicion that the thing that makes you think the keys have changed order is that Chrome devtools show objects with their keys sorted in alphabetical order. Whereas if you use Object.keys() or the equivalent JS to manually iterate through the keys, you will find they come out in the order they were defined in the JSON string.

这是Object.keys()的等效JS:

function objectKeys(obj) {
    var keys = [];
    if (!obj) return keys;
    for (var key in obj) {
        if (obj.hasOwnProperty(key)) {
            keys.push(key);
        }
    }
}

当我使用已解析对象的objects部分调用此函数时,将得到以下数组:

When I call this with the objects part of the parsed object I get the following array:

["Contact", "Account", "cnx__Phone__c"]

这篇关于解析对象后,保持对象在JSON字符串中的顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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