Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python中的JSON标记顺序_Python_Json_Tcp - Fatal编程技术网

Python中的JSON标记顺序

Python中的JSON标记顺序,python,json,tcp,Python,Json,Tcp,我是Python新手,在大学里一直在学习Python,我有一个问题。首先,这是有关守则的重要部分: #This is on my TCP client name = raw_input("Box name: ") timestamp = time.time() json_dict = {'type': 'CREATE', 'name': name, 'timestamp': timestamp}

我是Python新手,在大学里一直在学习Python,我有一个问题。首先,这是有关守则的重要部分:

#This is on my TCP client
    name = raw_input("Box name: ")
    timestamp = time.time()

    json_dict = {'type': 'CREATE',
                 'name': name,
                 'timestamp': timestamp}

    print json_dict
    tcp_s.send(pickle.dumps(json_dict))

#This is on my TCP server
    json_dict = pickle.loads(client_s.recv(65536))
    file_received = json.dumps(json_dict,indent=4)
    print file_received
我基本上是试图将信息从客户端传递到服务器,并将其转换为JSON,这是成功的。我的问题:它显示类型、名称和时间戳标记的顺序与我希望的不同。我想这可能是因为使用了没有顺序的字典,这就是我来这里的原因

电流输出:

{
    "timestamp": 1492173052.087644, 
    "type": "CREATE", 
    "name": "test"
}
有什么建议让输出像这样吗

{
    "type": "CREATE", 
    "name": "test"    
    "timestamp": 1492173052.087644,   
}

您已经发布了一个有效的数据结构,但这通常不是您想要的打印输出。顺序受dict行为的影响是正确的,但是如果您希望以特定的顺序打印值,那么您真的也需要封闭的大括号吗?如果不是这样,只需像使用字典一样使用键,以所需的顺序打印值即可。顺序无关紧要。JSON对象与Python字典一样,都是无序结构。为什么你需要执行特定的命令?是的@roganjosh我也想要花括号。我通常不会介意排序问题,但是这是针对一个将来实际上需要正确排序的项目。我同意这不是一个现实的期望,因为JSON的顺序或多或少是不相关的。然而,作为一个编程练习,我可以看到他们可能会要求它。这应该会有所帮助。