Python interface Automation (i)---JSON data type values

Source: Internet
Author: User
Tags first string

Contact Automation also has a period of time, before learning Selenium,appium, this time in learning interface Automation, last appeared a problem, is requests JSON parsing data, to choose data from the parsed data to do assertequal judgment, The result has baffled me, although said is a bit simple knowledge point, but if the foundation is not solid, or not careful person, will encounter me this kind of situation, finally asked the old driver, solves the problem.

Case review

The following interfaces are attached:

Http://tj.nineton.cn/Heart/index/all?city=CHSH000000&language=zh-chs&unit=c&aqi=city&alarm=1
This is a weather forecast interface, through postman parsing, will get a return value, using JSON parsing, will get a lot of data, then I select some of the data, plus a few tuples, the tuple is also integrated, to do an analysis.
The parsed data is attached below

R = {

"Status": "OK",

"Weather": [

{

"City_name": "Shanghai",

"Now": {

"Text": "Yin",

"Code": "9"

},

"Future": [

{

"Date": "2017-05-12",

"High": "22"

},

{

"Date": "2017-05-13",

"High": "30"

}

]

},

("Nanchang", "Changsha", "Hangzhou"),

["Shanghai", "Beijing", "ShenZhen"]

]

}

Then, we have to get the value of the dictionary in the status of OK, City_name for the value of Shanghai, date is 2017-05-13, there is a tuple inside the "Changsha", the list of "ShenZhen" value

Some people see, will be more confused, see so much, how do I value, or, may be due to their carelessness, do not see a certain symbol, or a few numbers, resulting in the failure of the test results, then, before writing the answer, we first parse the above data type.

{} Then it must be a dictionary, ("Nanchang", "Changsha", "Hangzhou") for tuples, ["Shanghai", "Beijing", "ShenZhen"] List

One, the meta-group review

Let's look at the tuples first,

A python tuple is similar to a list, except that the elements of a tuple cannot be modified. Tuples use parentheses, and the list uses square brackets. Tuple creation is simple, just add elements in parentheses and separate them with commas.

Example: Other_city = ("Nanchang", "Changsha", "Hangzhou")

Tuples are similar to strings, subscript index starting from 0, can be intercepted, combined, etc...

>>> r = ("Nanchang", "Changsha", "Hangzhou")

Get a single string
>>> R[0]
' Nanchang '

Intercepts the first and second strings
>>> R[0:2]
(' Nanchang ', ' Changsha ')

Get the first string of the countdown
>>> R[-1]
' Hangzhou '

Get all the strings
>>> R[0:]
(' Nanchang ', ' Changsha ', ' Hangzhou ')

Second, list review

Similarly, the list of lists is obtained in the same way as a tuple, so we can do a simple review.

R ["Nanchang", "Changsha", "Hangzhou"]
>>> R[0]
"Nanchang"

No more talking here.

Three, dictionary review

Next is the dictionary,

A dictionary is another mutable container model and can store any type of object.

Each key value of the dictionary (Key=>value) is split with a colon (:), each pair is separated by a comma (,), and the entire dictionary is enclosed in curly braces ({}) , as shown in the following format

D = {key1 : value1, key2 : value2 }

Let's give an example

>>> d = {"name": "Xiaoming", "Age": "$", "sexy": "Man"}
>>> d["Name"]
' Xiaoming '

Four, the answer analysis

Then, after analyzing the above information, we can begin to take the value, believe you see here, will feel, no matter how much data, the value will be very simple

1,status

Take the first value: status, which is the first in the dictionary, so it's good to take a value, is

>>> r["status"]
' OK '

2,city_name = Shanghai

Take the second value: City_name for Shanghai, which is a bit around, dict+list+dict, in fact, this analysis is very simple, then we began to step-by-step analysis it

Shanghai is inside the weather, so we take the value

r["Weather"]
[{' Now ': {' text ': ' Yin ', ' Code ': ' 9 '}, ' city_name ': ' Shanghai ', ' future ': [{' High ': ' + ', ' Date ': ' 2017-05-12 '}, {' High ': ' + ', ' d Ate ': ' 2017-05-13 '}]}, (' Nanchang ', ' Changsha ', ' Hangzhou '), (' Shanghai ', ' Beijing ', ' ShenZhen ')]

City_name is inside the first dict in the list, so we're taking a value

>>> r["Weather"][0]
{' Now ': {' text ': ' Yin ', ' Code ': ' 9 '}, ' city_name ': ' Shanghai ', ' future ': [{' High ': ' + ', ' Date ': ' 2017-05-12 '}, {' High ': ' + ', ' da Te ': ' 2017-05-13 '}]}

Shanghai in the dict of the city_name inside, so it is a simple three-step value taken out of the

>>> r["Weather"][0]["City_name"]
Shanghai

3,date = 2017-05-13

Now take the third value date is 2017-05-13, this is actually two more steps than the above, a list and a dict, I wrote the answer directly

>>> r["Weather"][0]["Future"][1]["date"]
' 2017-05-13 '

Values for 4,changsha and Shenzhen

Now take the fourth value, the "Changsha" inside the tuple

"Changsha" in the tuple, this tuple is inside the list, so we want to get to that tuple first

r["Weather"][1]
(' Nanchang ', ' Changsha ', ' Hangzhou ')

In the next step

r["Weather"][1][1]
' Changsha '

In the same vein, the value of "ShenZhen" in the list becomes very simple, and I write the answer directly.

>>> r["Weather"][2][2]
' ShenZhen '

Parsing the data is very simple, as long as a little foundation, you can parse, but the second value will be a bit around, dict+list+dict+list, after an analysis, will find that, in fact, not too difficult, step by step analysis, do the test is not to find the bug, but also to analyze the reasons, you will be more effective.

Finally, attach all the code

R = {
"Status": "OK",
"Weather": [
{
"City_name": "Shanghai",
"Now": {
"Text": "Yin",
"Code": "9"
},
"Future": [
{
"Date": "2017-05-12",
"High": "22"
},
{
"Date": "2017-05-13",
"High": "30"
}
]
},
("Nanchang", "Changsha", "Hangzhou"),
("Shanghai", "Beijing", "ShenZhen")
]
}

>>> r["status"]
' OK '


>>> r["Weather"]
[{' Now ': {' text ': ' Yin ', ' Code ': ' 9 '}, ' city_name ': ' Shanghai ', ' future ': [{' High ': ' + ', ' Date ': ' 2017-05-12 '}, {' High ': ' + ', ' d Ate ': ' 2017-05-13 '}]}, (' Nanchang ', ' Changsha ', ' Hangzhou '), (' Shanghai ', ' Beijing ', ' ShenZhen ')]


>>> r["Weather"][0]
{' Now ': {' text ': ' Yin ', ' Code ': ' 9 '}, ' city_name ': ' Shanghai ', ' future ': [{' High ': ' + ', ' Date ': ' 2017-05-12 '}, {' High ': ' + ', ' da Te ': ' 2017-05-13 '}]}


>>> r["Weather"][0]["City_name"]
Shanghai

>>> r["Weather"][0]

{' Now ': {' text ': ' Yin ', ' Code ': ' 9 '}, ' city_name ': ' Shanghai ', ' future ': [{' High ': ' + ', ' Date ': ' 2017-05-12 '}, {' High ': ' + ', ' da Te ': ' 2017-05-13 '}]}


>>> r["Weather"][0]["future"]
[{' High ': ' + ', ' Date ': ' 2017-05-12 '}, {' High ': ' + ', ' Date ': ' 2017-05-13 '}]

>>> r["Weather"][0]["Future"][1]["date"]
' 2017-05-13 '


>>> r["Weather"][1][1]
' Changsha '
>>> r["Weather"][2][2]
' ShenZhen '

Non-reprint and commercial use, only as a study material, offenders must investigate

Python interface Automation (i)---JSON data type values

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.