当我从数据文件中获取数据时,我的 json 请求的顺序按字母顺序排序.我不想让我的 json 请求排序 [英] Oder of my json request is getting sorted in alphabetical order when i am getting data from data file.i dnt want my json request to get sorted

查看:96
本文介绍了当我从数据文件中获取数据时,我的 json 请求的顺序按字母顺序排序.我不想让我的 json 请求排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

{
   "ID":0,
   "OrganizationId":"",
   "OrganizationName":"",
   "Name":"",
   "IsActive":"True",
   "Type":2,
   "AppliesTo":1,
   "TagHOD":"",
   "DisplayAsPrimary":"false",
   "Values":[

   ]
}

以上是我存储在数据文件中的 json 请求

Above is my json request which I have stored in a data file

下面是我在向其中发送参数后得到的 json 请求正文.它按字母顺序排序,我不想要.我想要与上面相同的顺序,例如 ID 应该是第一个,然后是 OrganizationId

Below is my json request body which I am getting after sending a parameter into it. It is sorted into alphabetical order which I don't want. I want the same order as above eg ID Should be first then OrganizationId

{
    "AppliesTo": 1,
    "DisplayAsPrimary": "false",
    "ID": 0,
    "IsActive": "True",
    "Name": "TAG1205510333275",
    "OrganizationId": 2404,
    "OrganizationName": "",
    "TagHOD": "",
    "Type": 2,
    "Values": [
        {
            "HODEmail": "tagsapiautomationae@mailinator.com",
            "Id": 1,
            "IsDeleted": false,
            "Text": "Level20"
        }
    ]
}

推荐答案

JSON 规范声明:一个对象是一组无序的名称/值对."

The JSON specification states: "An object is an unordered set of name/value pairs."

使用 JSON(以及大多数语言中的对象)时,对象中的属性本质上是无序的.您不能依赖不同的系统按照您提供的相同顺序为您提供属性.事实上,您甚至不能依赖单个系统在给定的代码执行中始终以相同的顺序为您提供属性,即使许多系统确实这样做.

When working with JSON (and objects in most languages), the properties in objects are inherently unordered. You can't rely on different systems giving you the properties in the same order you supply them. In fact, you can't even rely on a single system giving you the properties in the same order all the time within a given execution of the code, even though many systems do behave that way.

如果你想保持排序,你要么需要使用一个数组来存储数据,或者你可以使用一个对象属性名称数组来按照你想要的顺序存储键,这样你就可以使用该数组来引用稍后按所需顺序排列它们.

If you want to preserve ordering, you either need to use an array to store the data, or you can use an array of object property names that stores the keys in the order you want, so you can use that array to reference them in the desired order later.

EG:

   keyorder = ["ID",
               "OrganizationId",
               "OrganizationName",
               "Name",
               "IsActive",
               "Type",
               "AppliesTo",
               "TagHOD",
               "DisplayAsPrimary",
               "Values"
               ]

然后您可以在访问对象中的元素时循环访问该数组,因此您始终可以按照定义的顺序访问它们.

You can then loop over this array when accessing elements in your object, so you are always accessing them in your defined order.

在 python 中,一个名为data"的对象如下所示:

In python, with an object named "data" this would look like:

 for key in keyorder:
      print data.get(key)

这篇关于当我从数据文件中获取数据时,我的 json 请求的顺序按字母顺序排序.我不想让我的 json 请求排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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