Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Characteristics and example usage of Json data in Python

2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "the characteristics and example usage of Json data in Python". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn "the characteristics and example usage of Json data in Python".

Introduction to Json

Json (JavaScript Object Notation)

Many websites use Json format for data transmission and exchange, just like the NetEYun music interface I mentioned in the previous article, the data they return is in Json format.

This is because Json is a lightweight data exchange format, which has many advantages, such as simple data format, easy to read and write and so on. It is used for front-end and back-end data transmission, which greatly simplifies the development workload of server and client.

And compared with XML, it is more lightweight and more convenient to parse.

Today we talk about how to play with Json data in python?

In Json, follow the "key-value" approach.

For example, the simplest one: "{" name ":" zhuxiao5 "}", like the dictionary in python, is also a data in Json format.

Something more complex, such as this one (this example will be used many times later):

Taking the above picture as an example, let's say a few more words about the characteristics of Json format.

Objects are represented by key-value pairs

The key is wrapped in double quotes, followed by a colon ":", followed by the value of the key

Values can be strings, numbers, arrays, and other data types

Objects are separated by commas

"{}" is used to express objects

"[]" is used to express an array

Json module in Python

Json module is also included in Python, in which json.dumps () and json.loads () are more commonly used.

Json.dumps () converts the python object to json.

Json.loads () converts json to a python object.

Running result:

In the example, the initial variable dict_data is a dictionary, and after json.dumps (), the dict format data is converted to an json format string. At this time, although they are all {'averse: 1,' baked: 2}, the format is different. The json format string is then re-converted into a dictionary through json.loads ().

Online parsing of Json

In practical applications, to extract json data, it is necessary to understand the structure of the returned json data.

But the data in Json format is often like this.

Don't worry, we can copy the data to some json plug-ins or parse online!

For example, this plug-in is commonly used by Primary five:

Open the URL again at this time.

Is it much clearer?

What do you do if you use python to get the data inside?

First use json.loads () to convert Json into a dictionary, and then use the get () function until we get the desired list object, then we can use a for loop for the data in list.

Well, it's a little roundabout.

As an example at the beginning of the article, we want to get the names of all the dogs:

We can do this:

Running result:

This can really get the results we want.

PS: there are many similar online parsing sites, such as https://www.json.cn.

JsonPath

I don't know if you remember, when I first introduced Json, I mentioned that it is more lightweight and easier to parse than XML.

Since all XML families have XPATH, does Json have similar tools?

JsonPath is an information extraction class library, which is a tool for extracting specified information from Json documents.

JsonPath for Json is equivalent to XPATH for XML.

Json has a clear structure, high readability, low complexity and easy to match. The following table shows the usage of JsonPath.

Yes, for the same example, we tried to use JsonPath to get the names of all the dogs:

We can do this:

Where $.. name means to start from the root node and match the name node

Running result:

We can also get the results we want with JsonPath.

We will continue to use JsonPath to extract data in subsequent example walkthroughs.

Case drills

Example: we use NetEase Cloud Music Review API to generate Json data and obtain hot review data from it.

Open in a browser with the Json parsing plug-in installed:

The data in the red area is what we want to get this time.

Running result:

Successfully obtained √

At this point, I believe you have a deeper understanding of the "characteristics and example usage of Json data in Python". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report