<legend id='Swr36'><style id='Swr36'><dir id='Swr36'><q id='Swr36'></q></dir></style></legend>
  • <small id='Swr36'></small><noframes id='Swr36'>

    <tfoot id='Swr36'></tfoot>

    <i id='Swr36'><tr id='Swr36'><dt id='Swr36'><q id='Swr36'><span id='Swr36'><b id='Swr36'><form id='Swr36'><ins id='Swr36'></ins><ul id='Swr36'></ul><sub id='Swr36'></sub></form><legend id='Swr36'></legend><bdo id='Swr36'><pre id='Swr36'><center id='Swr36'></center></pre></bdo></b><th id='Swr36'></th></span></q></dt></tr></i><div id='Swr36'><tfoot id='Swr36'></tfoot><dl id='Swr36'><fieldset id='Swr36'></fieldset></dl></div>

    • <bdo id='Swr36'></bdo><ul id='Swr36'></ul>
      1. 转换“真"(JSON) 到 Python 等效的“真"

        Converting quot;truequot; (JSON) to Python equivalent quot;Truequot;(转换“真(JSON) 到 Python 等效的“真)
        <i id='4LIro'><tr id='4LIro'><dt id='4LIro'><q id='4LIro'><span id='4LIro'><b id='4LIro'><form id='4LIro'><ins id='4LIro'></ins><ul id='4LIro'></ul><sub id='4LIro'></sub></form><legend id='4LIro'></legend><bdo id='4LIro'><pre id='4LIro'><center id='4LIro'></center></pre></bdo></b><th id='4LIro'></th></span></q></dt></tr></i><div id='4LIro'><tfoot id='4LIro'></tfoot><dl id='4LIro'><fieldset id='4LIro'></fieldset></dl></div>

            <tbody id='4LIro'></tbody>

          <small id='4LIro'></small><noframes id='4LIro'>

                <legend id='4LIro'><style id='4LIro'><dir id='4LIro'><q id='4LIro'></q></dir></style></legend>
                <tfoot id='4LIro'></tfoot>
                  <bdo id='4LIro'></bdo><ul id='4LIro'></ul>
                  本文介绍了转换“真"(JSON) 到 Python 等效的“真"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我最近使用的 Train status API 在 JSON 对象中添加了两个额外的键值对 (has_arrived, has_departed),导致我的脚本崩溃.

                  The Train status API I use recently added two additional key value pairs (has_arrived, has_departed) in the JSON object, which caused my script to crash.

                  这是字典:

                  {
                  "response_code": 200,
                    "train_number": "12229",
                    "position": "at Source",
                    "route": [
                      {
                        "no": 1,
                        "has_arrived": false,
                        "has_departed": false,
                        "scharr": "Source",
                        "scharr_date": "15 Nov 2015",
                        "actarr_date": "15 Nov 2015",
                        "station": "LKO",
                        "actdep": "22:15",
                        "schdep": "22:15",
                        "actarr": "00:00",
                        "distance": "0",
                        "day": 0
                      },
                      {
                        "actdep": "23:40",
                        "scharr": "23:38",
                        "schdep": "23:40",
                        "actarr": "23:38",
                        "no": 2,
                        "has_departed": false,
                        "scharr_date": "15 Nov 2015",
                        "has_arrived": false,
                        "station": "HRI",
                        "distance": "101",
                        "actarr_date": "15 Nov 2015",
                        "day": 0
                      }
                    ]
                  }
                  

                  不出所料,我收到以下错误:

                  Not surprisingly, I got the following error:

                  Traceback (most recent call last):
                    File "<stdin>", line 1, in <module>
                  NameError: name 'false' is not defined
                  

                  如果我没记错的话,我认为这是因为 JSON 响应中的布尔值是 false/true 而 Python 识别 False/.有什么办法解决吗?

                  If I am not mistaken, I think this is because the boolean value in the JSON response is false/true whereas Python recognizes False/True. Is there any way around it?

                  PS:我尝试将 has_arrived 的 JSON 响应转换为字符串,然后将其转换回布尔值,结果发现我总是会得到一个 True 值,如果字符串中有任何字符.我有点卡在这里.

                  PS: I tried converting the JSON response of has_arrived to string and then converting it back to a boolean value, only to find out that I'll always get a True value if there's any character in the string. I am kinda stuck here.

                  推荐答案

                  尽管 Python 的对象声明语法与 Json 语法非常相似,但它们却截然不同且不兼容.除了 True/true 问题之外,还有其他问题(例如,Json 和 Python 处理日期的方式非常不同,python 允许单引号和注释,而 Json 不允许).

                  Even though Python's object declaration syntax is very similar to Json syntax, they're distinct and incompatible. As well as the True/true issue, there are other problems (eg Json and Python handle dates very differently, and python allows single quotes and comments while Json does not).

                  与其试图将它们视为同一事物,解决方案是根据需要从一个转换为另一个.

                  Instead of trying to treat them as the same thing, the solution is to convert from one to the other as needed.

                  Python 的原生 json 库可用于解析(读取)字符串中的 Json 并将其转换为 python 对象,并且您已经安装了它...

                  Python's native json library can be used to parse (read) the Json in a string and convert it into a python object, and you already have it installed...

                  # Import the library
                  import json
                  # Define a string of json data
                  data_from_api = '{"response_code": 200, ...}'
                  info = json.loads(data_from_api)
                  # info is now a python dictionary (or list as appropriate) representing your Json
                  

                  您也可以将 python 对象转换为 json...

                  You can convert python objects to json too...

                  info_as_json = json.dumps(info)
                  

                  例子:

                  # Import the json library
                  import json
                  
                  # Get the Json data from the question into a variable...
                  data_from_api = """{
                  "response_code": 200,
                    "train_number": "12229",
                    "position": "at Source",
                    "route": [
                      {
                        "no": 1, "has_arrived": false, "has_departed": false,
                        "scharr": "Source",
                        "scharr_date": "15 Nov 2015", "actarr_date": "15 Nov 2015",
                        "station": "LKO", "actdep": "22:15", "schdep": "22:15",
                        "actarr": "00:00", "distance": "0", "day": 0
                      },
                      {
                        "actdep": "23:40", "scharr": "23:38", "schdep": "23:40",
                        "actarr": "23:38", "no": 2, "has_departed": false,
                        "scharr_date": "15 Nov 2015", "has_arrived": false,
                        "station": "HRI", "distance": "101",
                        "actarr_date": "15 Nov 2015", "day": 0
                      }
                    ]
                  }"""
                  
                  # Convert that data into a python object...
                  info = json.loads(data_from_api)
                  print(info)
                  

                  第二个例子展示了真/真转换是如何发生的.还要注意引用的变化以及评论是如何被剥离的......

                  And a second example showing how the True/true conversion happens. Note also the changes to quotation and how the comment is stripped...

                  info = {'foo': True,  # Some insightful comment here
                          'bar': 'Some string'}
                  
                  # Print a condensed representation of the object
                  print(json.dumps(info))
                  
                  > {"bar": "Some string", "foo": true}
                  
                  # Or print a formatted version which is more human readable but uses more bytes
                  print(json.dumps(info, indent=2))
                  
                  > {
                  >   "bar": "Some string",
                  >   "foo": true
                  > }
                  

                  这篇关于转换“真"(JSON) 到 Python 等效的“真"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Initialize Multiple Numpy Arrays (Multiple Assignment) - Like MATLAB deal()(初始化多个 Numpy 数组(多重赋值) - 像 MATLAB deal())
                  How to extend Python class init(如何扩展 Python 类初始化)
                  What#39;s the difference between dict() and {}?(dict() 和 {} 有什么区别?)
                  What is a wrapper_descriptor, and why is Foo.__init__() one in this case?(什么是 wrapper_descriptor,为什么 Foo.__init__() 在这种情况下是其中之一?)
                  Initialize list with same bool value(使用相同的布尔值初始化列表)
                  setattr with kwargs, pythonic or not?(setattr 与 kwargs,pythonic 与否?)

                    <small id='ub0F2'></small><noframes id='ub0F2'>

                      <bdo id='ub0F2'></bdo><ul id='ub0F2'></ul>
                    • <i id='ub0F2'><tr id='ub0F2'><dt id='ub0F2'><q id='ub0F2'><span id='ub0F2'><b id='ub0F2'><form id='ub0F2'><ins id='ub0F2'></ins><ul id='ub0F2'></ul><sub id='ub0F2'></sub></form><legend id='ub0F2'></legend><bdo id='ub0F2'><pre id='ub0F2'><center id='ub0F2'></center></pre></bdo></b><th id='ub0F2'></th></span></q></dt></tr></i><div id='ub0F2'><tfoot id='ub0F2'></tfoot><dl id='ub0F2'><fieldset id='ub0F2'></fieldset></dl></div>

                            <tbody id='ub0F2'></tbody>

                          1. <tfoot id='ub0F2'></tfoot>

                            <legend id='ub0F2'><style id='ub0F2'><dir id='ub0F2'><q id='ub0F2'></q></dir></style></legend>