Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
569 views
in Technique[技术] by (71.8m points)

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

I receive the following JSON string from an API function.

(我从API函数收到以下JSON字符串。)

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

The order of the objects inside "objects" is important!

("objects""objects"的顺序很重要!)

But when i parse this JSON string with JSON.parse , the order of those objects is lost.

(但是,当我使用JSON.parse解析此JSON字符串时,这些对象的顺序将丢失。)

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

(有什么好的方法可以在解析完这些对象后保持其顺序。)

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.

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

  ask by Praind translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

(我怀疑使您认为键已更改顺序的是Chrome devtools显示对象,其键按字母顺序排序。)

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手动遍历密钥,则会发现它们按照在JSON字符串中定义的顺序出现。)

Chrome devtools的屏幕截图

Here is the equivalent JS for Object.keys() :

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

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

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

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

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...