Day18--json

Source: Internet
Author: User

Introduction to the use of file JSON JSON: JSON, full name JavaScript Object Notation, is a lightweight data interchange format. The most extensive application of JSON is the data format for the communication of Web servers and clients in Ajax. It is also often used in HTTP requests, so it is natural to learn a variety of JSON. And we're going to take the two most long-run ways we usually work. JSON online parsing and formatting URLs: json.cn

Python Json
Dict Object
List, tuple Array
STR, Unicode String
int, long, float Number
True True
False False
None Null
#json有四种方法: # json.loads# json.dumps# Multi-S is to deal with the string, no more s is to process the file's # json.load# Json.dump #load或者loads word means: load, is to convert JSON into other formats, such as strings or file-related #dump or dumps words that mean upside down, which is to convert other objects or formats into JSON format
#Example 1: Convert Python dict format to JSON string formatImportJsona= Dict (name ='yangling', age = 28,message ='You is so cool')Print(a)Print(Type (a)) b=Json.dumps (a)Print(b)Print(type (b))#Example 2:c =Json.loads (b)Print(Type (c))Print((c))Print(c['name'])

Printing results are:

2. Conversion between file and JSON import JSON #文件相关的话 #load must be out of the file JSON data, load must be to convert the file to JSON data #dump is to write the JSON data into the file # write the JSON into the file dump
1Jsondata ='{\ "a\": 1,\ "b\": 2,\ "c\": 3,\ "d\": 4,\ "e\": 5}'2With open ('a.txt','W') as F:3 json.dump (jsondata,f)4 5With open ('a.txt','R') as fr:6m =Json.load (FR)7 Print(m)8 Print(Type (m))

Printing results are:

Write a a.txt file with the contents: "{\" a\ ": 1,\" b\ ": 2,\" c\ ": 3,\" d\ ": 4,\" e\ ": 5}"

Day18--json

Related Article

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.