如何在JSON中查找类 [英] how to find a class in JSON
问题描述
tabGTD {
Class: Stream class
Type: Receipt type
Date :Current date
PLUs [] {
{
No: PLU number
}
}
}
这是一个示例json脚本.我正在使用system.net.json参考.....,我必须从json中查找"tabGTD"和类,类型.(我可以将其作为字段和值来找到.)帮助我查找标头类和每个字段. /p>
this is a sample json script. am using system.net.json reference..... and i have to find "tabGTD" and class,type from json.( i can find it as field and value.) help me to find header class and each field.
推荐答案
Python包含了相当不错的JSON处理接口.
http://docs.python.org/library/json.html
Python has pretty decent JSON handling interface included.
http://docs.python.org/library/json.html
我想像这样的json:
I'd imagine json like this:
tabGTD ='{"Class":"Stream class", 类型":收据类型",日期": 当前日期","PLU":{否":"PLU 数字}}'
tabGTD= '{ "Class": "Stream class", "Type": "Receipt type", "Date": "Current date", "PLUs": {"No": "PLU number"} }'
然后您可以通过以下方式将其加载到python中:
Then you can get it loaded in python like this way:
>>> tabGTD= '{"Class": "Stream class","Type": "Receipt type","Date": "Current da
te","PLUs":{"No": "PLU number"}}'
>>> a=json.loads(tabGTD)
>>> a
{'Date': 'Current date', 'PLUs': {'No': 'PLU number'}, 'Type': 'Receipt type', '
Class': 'Stream class'}
>>> a['Class']
'Stream class'
>>> [a['Class'],a['Type']]
['Stream class', 'Receipt type']
编辑
因此,您需要在json数据中包含tabGTD密钥:
EDIT
So you need to have tabGTD key in your json data:
>>> someJson= '{"tabGTD":{"Class": "Stream class","Type": "Receipt type","Date":
"Current date","PLUs":{"No": "PLU number"}}}'
>>> someJson
'{"tabGTD":{"Class": "Stream class","Type": "Receipt type","Date": "Current date
","PLUs":{"No": "PLU number"}}}'
>>> a=json.loads(someJson)
>>> a
{'tabGTD': {'Date': 'Current date', 'PLUs': {'No': 'PLU number'}, 'Type': 'Recei
pt type', 'Class': 'Stream class'}}
>>> a.keys()
['tabGTD']
>>> 'tabGTD' in a.keys()
True
>>> if 'tabGTD' in a.keys():
... tGTD=a['tabGTD'];
...
>>> tGTD
{'Date': 'Current date', 'PLUs': {'No': 'PLU number'}, 'Type': 'Receipt type', '
Class': 'Stream class'}
>>> tGTD['Type']
'Receipt type'
>>>
您可以在此处找到有关JSON格式的一些信息: json desc
You can find some information on JSON format here: json desc
这篇关于如何在JSON中查找类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!