程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

JSON object processing in Python

編輯:Python

python And json Object processing

  • One 、json Definition
    • 1.1 Concept
    • 1.2 Basic grammar
    • 1.3 json Follow python The difference between dictionaries in
  • Two 、json And python Interaction between
    • 2.1 Python Handle json Module :json
    • 2.2 example
      • 2.2.1 Translate a dictionary into json strand
      • 2.2.2 Python decode JSON object
      • 2.2.3 Read json file
      • 2.2.4 write in json file
    • 2.3 Conversion correspondence
      • 2.3.1 Python Type conversion JSON Type correspondence
      • 2.3.2 json Type to Python Type comparison table of
  • 3、 ... and 、 After reading json It can be interpreted as python Of DataFrame

One 、json Definition

1.1 Concept

  • JSON:JavaScript Object Notation 【JavaScript Object notation 】
    JSON Is a lightweight data exchange format , Text formats that are completely independent of any programming language . commonly , The background application encapsulates the response data into JSON Format return .

1.2 Basic grammar

  • JSON The basic grammar is as follows :JSON The most common format is the key value pair of an object :key Can only be string, value It can be object、array、string、number、true/false、null.
  • As shown below :
    {
    “sites”: [
    { “name”:“360” , “url”:“www.360.com” },
    { “name”:“google” , “url”:“www.google.com” },
    { “name”:“baidu” , “url”:“www.baidu.com” }
    ]
    }
    1) The key is wrapped in double quotation marks , Followed by a colon “:”, And then the value of that key ;
    2) The value can be a string 、 Numbers 、 Array and other data types ;
    3) Objects are separated by commas ;
    4)“{}” To save objects ;
    5)“[]” Used to save arrays ;

1.3 json Follow python The difference between dictionaries in

1)json Of key It can only be a string ,dict Of key It can be anything hash The object of , for example : character string 、 Numbers 、 Tuples etc. ;

2) A dictionary is a data structure ,json Is a data format ; Dictionaries have many built-in functions , There are many ways to call , and json It's a format for data packaging , It's not as operational as a dictionary ;

3)json Force double quotation marks on the string of ,dict The string of can be in single quotes 、 Double quotes ;

generally speaking , We will reduce the json Turn into python Dictionaries or lists in , And then operate it .

Two 、json And python Interaction between

2.1 Python Handle json Module :json

  • Pythone3 The standard library of JSON modular , It's very convenient to help us with json Data conversion and processing , Here we mainly refer to serialization (json.dumps()、json.dump()) And deserialization (json.loads()、json.load()).
  • Serialization and deserialization :
    Convert objects to data formats that can be transferred over the network or stored on local disks ( Such as :XML、JSON Or a particular format of byte string ) This process is called serialization ; conversely , Is called deserialization .
  • frequently-used JSON Module method :
    1)json.dumps(): take Python The object in is converted to JSON String object in ;
    2)json.dump(): take python Object conversion to JSON String output to fp Streaming ;
    3)json.loads(): take JSON The string object in is converted to Python Objects in the ;
    4)json.load(): Read contains json Object file .

belt s It's all about strings , No s It's all about documents .

2.2 example

2.2.1 Translate a dictionary into json strand

import json
dic = {
'name': 'xiaoming', 'age': 29}
json_str = json.dumps(dic)# return json character string 
print(json_str)
print(type(json_str))
# result 
{
"name": "xiaoming", "age": 29}
<class 'str'>

2.2.2 Python decode JSON object

import json
json_str ='{"id":"09", "name": "Nitin", "department":"Finance"}'
# Convert string to Python dict
dic = json.loads(json_str)
print(dic)
print(type(dic))
# result 
{
'id': '09', 'name': 'Nitin', 'department': 'Finance'}
<class 'dict'>

2.2.3 Read json file

import json
with open('test.json') as f:
a = json.load(f)
print(a)
print(type(a))
# result 
{
'id': '09', 'name': 'Nitin', 'department': 'Finance'}
<class 'dict'>

2.2.4 write in json file

import json
dic ={

"name" : "xiaoming",
"age" : 20,
"phonenumber" : "15555555555"
}
with open("test2.json", "w") as outfile:
json.dump(dic, outfile)

2.3 Conversion correspondence

2.3.1 Python Type conversion JSON Type correspondence

2.3.2 json Type to Python Type comparison table of

3、 ... and 、 After reading json It can be interpreted as python Of DataFrame

  • pandas.json_normalize Function to parse , Will be semi-structured JSON The data is normalized to a flat table .
  • Code :
# Read json file 
import pandas as pd
import json
with open('results.json') as f:
results = json.load(f)

# Read the corresponding fields 
FIELDS = ["key", "fields.summary", "fields.issuetype.name", "fields.status.name", "fields.status.statusCategory.name"]
df = pd.json_normalize(results["issues"])
df[FIELDS]

# Use paths instead of directly results["issues"]
pd.json_normalize(results, record_path="issues")[FIELDS]

# Custom delimiter : use "-" Replace the default "."
# Use sep Parameter to define the separator of nested structure connection 
FIELDS = ["key", "fields-summary", "fields-issuetype-name", "fields-status-name", "fields-status-statusCategory-name"]
pd.json_normalize(results["issues"], sep = "-")[FIELDS]

# Control recursion 
# If you don't want to recurs to every child object , have access to max_level Parameters control depth . under these circumstances , because statusCategory.name The field is located in JSON Object number 4 level , So it won't be included in the result DataFrame in .
# Only drill down to the second level of nesting 
pd.json_normalize(results, record_path="issues", max_level = 2)


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved