23.23. Python JSON conversion

Source: Internet
Author: User
Tags string format

JSON ==> Javascript Object Notation

AJAX is a form of key:value.

There are four methods of JSON:

Json.loads

Json.dumps

Understanding: Plus S is used to handle strings.

Json.load

Json.dump

Understanding: Not adding S is used to process the file


Load or loads

Convert JSON to other objects, strings, or file-related.


Dump or dumps

To convert another object or format into a JSON format

1. Conversion between strings and JSON

Example 1: Converting a Python dictionary format into a JSON string format

>>> a = dict (name= ' Linan ', age= ' + ', message= ' yes ') >>> print a{' message ': ' Yes ', ' age ': ' + ', ' name ': ' Linan '}>>> print type (a) <type ' dict ' >>>> import json>>> b=json.dumps (a) >>> Print b{"message": "Yes", "age": "All", "name": "Linan"}>>> print type (b) <type ' str ' >

Print A and print B are the same, but the format has changed


https://www.json.cn/ This site supports JSON parsing


Example 2: Converting a string into a dictionary

>>> C = json.loads (b) >>> print c{u ' message ': U ' yes ', U ' age ': U ' ", U ' name ': U ' linan '}>>> prin T type (c) <type ' Dict ' >


2. Conversion between file and JSON

The load must have been out of the file. JSON data, converted to JSON data

Dump is the writing of JSON data to a file


Converts a string to a file in JSON format

Import jsonjsondata= ' {' A ': 1, ' B ': 2, ' C ': 3, ' d ': 4, ' E ': 5} ' ' # #字符串文件内写成三引号或者单引号with open (' A.txt ', ' W ') as F:json.dump ( Jsondata, F)

Results

The contents of the A.txt file are as follows:

"{\" a\ ": 1,\" b\ ": 2,\" c\ ": 3,\" d\ ": 4,\" e\ ": 5}"

Therefore, Json.dump () can write JSON data directly to the file.


Converts the contents of a JSON-formatted file into a string

With open (' A.txt ', ' R ') as Fr:m = Json.load (FR) print (m) print (Type (m))

Results

{"A": 1, "B": 2, "C": 3, "D": 4, "E": 5}

<type ' Unicode ' >

So, the Json.load () bar file content is converted to a Unicode data type to return


This article is from the "Note space" blog, so be sure to keep this source http://286577399.blog.51cto.com/10467610/1981382

23.23. Python JSON conversion

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.