more pythonic way to format a JSON string from a list of tuples

后端 未结 4 1434
清歌不尽
清歌不尽 2021-01-12 11:05

Currently I\'m doing this:

def getJSONString(lst):
    join = \"\"
    rs = \"{\"
    for i in lst:
        rs += join + \'\"\' + str(i[0]) + \'\":\"\' + str         


        
4条回答
  •  鱼传尺愫
    2021-01-12 11:45

    I think this is a much simple solution. Courtesy w3 schools https://www.w3schools.com/python/python_json.asp

    import json
    

    some JSON:

    x =  '{ "name":"John", "age":30, "city":"New York"}'
    

    parse x:

    y = json.loads(x)
    

    the result is a Python dictionary:

    print(y["age"])
    

提交回复
热议问题