使用Python从JSON获取值

时间:2022-03-09 21:15:08

While I am trying to retrieve values from JSON string, it gives me an error:

当我试图从JSON字符串中检索值时,它给了我一个错误:

data = json.loads('{"lat":444, "lon":555}')
return data["lat"]

But, if I iterate over the data, it gives me the elements (lat and lon), but not the values:

但是,如果我遍历数据,它会给出元素(lat和lon),但不会给出值:

data = json.loads('{"lat":444, "lon":555}')
    ret = ''
    for j in data:
        ret = ret + ' ' + j
return ret

Which returns: lat lon

返回:lat朗

What do I need to do to get the values of lat and lon? (444 and 555)

我需要做什么才能得到lat和lon的值?(444和555)

5 个解决方案

#1


55  

If you want to iterate over both keys and values of the dictionary, do this:

如果您想要遍历字典的键和值,请执行以下操作:

for key, value in data.items():
    print key, value

#2


24  

What error is it giving you?

它给了你什么错误?

If you do exactly this:

如果你这样做:

data = json.loads('{"lat":444, "lon":555}')

Then:

然后:

data['lat']

SHOULD NOT give you any error at all.

不应该给你任何错误。

#3


2  

Using your code, this is how i would do it, i know an answer was chosen, just giving additional options.

使用你的代码,这就是我要做的,我知道我选择了一个答案,只是提供了额外的选项。

data = json.loads('{"lat":444, "lon":555}')
    ret = ''
    for j in data:
        ret = ret+" "+data[j]
return ret

When you use for in this manor you get the key of the object, not the value, so you can get the value, by using the key as an index.

当您在这个庄园中使用时,您将获得对象的键,而不是值,因此您可以通过使用键作为索引来获取值。

#4


2  

There's a Py library that has a module that facilitates access to Json-like dictionary key-values as attributes: https://github.com/asuiu/pyxtension You can use it as:

有一个Py库,它有一个模块可以方便地访问类似json的字典键值作为属性:https://github.com/asuiu/pyxtension

j = Json('{"lat":444, "lon":555}')
j.lat + ' ' + j.lon

#5


2  

Using Python to extract a value from the provided Json

使用Python从提供的Json中提取一个值。

Working sample:-

import json
import sys

//load the data into an element
data={"test1" : "1", "test2" : "2", "test3" : "3"}

//dumps the json object into an element
json_str = json.dumps(data)

//load the json to a string
resp = json.loads(json_str)

//print the resp
print (resp)

//extract an element in the response
print (resp['test1'])

#1


55  

If you want to iterate over both keys and values of the dictionary, do this:

如果您想要遍历字典的键和值,请执行以下操作:

for key, value in data.items():
    print key, value

#2


24  

What error is it giving you?

它给了你什么错误?

If you do exactly this:

如果你这样做:

data = json.loads('{"lat":444, "lon":555}')

Then:

然后:

data['lat']

SHOULD NOT give you any error at all.

不应该给你任何错误。

#3


2  

Using your code, this is how i would do it, i know an answer was chosen, just giving additional options.

使用你的代码,这就是我要做的,我知道我选择了一个答案,只是提供了额外的选项。

data = json.loads('{"lat":444, "lon":555}')
    ret = ''
    for j in data:
        ret = ret+" "+data[j]
return ret

When you use for in this manor you get the key of the object, not the value, so you can get the value, by using the key as an index.

当您在这个庄园中使用时,您将获得对象的键,而不是值,因此您可以通过使用键作为索引来获取值。

#4


2  

There's a Py library that has a module that facilitates access to Json-like dictionary key-values as attributes: https://github.com/asuiu/pyxtension You can use it as:

有一个Py库,它有一个模块可以方便地访问类似json的字典键值作为属性:https://github.com/asuiu/pyxtension

j = Json('{"lat":444, "lon":555}')
j.lat + ' ' + j.lon

#5


2  

Using Python to extract a value from the provided Json

使用Python从提供的Json中提取一个值。

Working sample:-

import json
import sys

//load the data into an element
data={"test1" : "1", "test2" : "2", "test3" : "3"}

//dumps the json object into an element
json_str = json.dumps(data)

//load the json to a string
resp = json.loads(json_str)

//print the resp
print (resp)

//extract an element in the response
print (resp['test1'])