Python 3.x and Python 3.xjson

Source: Internet
Author: User

Python 3.x and Python 3.xjson

Preface

This article mainly introduces some operations of python3 on JSON and shares them for your reference. I will not talk about them here. Let's take a look at the details.

1. Convert Dictionary to JSON

Convert dict to JSON. The json package is used here.

Import jsonaItem = {} aItem ["id"] = "2203" aItem ["title"] = "title" aItem ["subTitle"] = "sub title" bItem = {} bItem ["id"] = "2842" bItem ["title"] = "B title" bItem ["subTitle"] = "B subTitle" bItem ["content"] =" content "bItem [" list "] = [" ", "a 2", "B", "bb"] aJson = json. dumps (aItem) bJson = json. dumps (bItem, ensure_ascii = False) print (aItem) print (aJson) print (bJson)

When it involves Chinese characters, you must specifyensure_ascii=False

Output:

{'Id': '000000', 'title': 'title', 'title': 'sub title'} {"id": "2203", "title ": "title", "subTitle": "sub title"} {"id": "2842", "title": "B title", "subTitle": "B subTitle ", "content": "content", "list": ["a", "a 2", "B", "bb"]}

Ii. Convert list to JSON

Code

jsonList = []jsonList.append(aItem)jsonList.append(bItem)jsonArr = json.dumps(jsonList, ensure_ascii=False)print(jsonArr)

Output:

[{"Id": "2203", "title": "title", "subTitle": "sub title" },{ "id": "2842 ", "title": "B title", "subTitle": "B subTitle", "content": "content"}]

This JSON string can be converted using the plug-in GsonFormat in Android Studio to obtain the corresponding object.

Summary

The above is all the content of this article. I hope the content of this article will help you in your study or work. If you have any questions, please leave a message, thank you for your support.

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.