JSON
JSON (JavaScript Object Notation) is a way of expressing information.[1] JSON is usually easy to understand. It can express information like XML.[…
JSON (JavaScript Object Notation) is a way of expressing information.[1] JSON is usually easy to understand. It can express information like XML.[2] It is based on JavaScript's notation for object literals.[3] However, JSON is stricter.[3]
JSON and XML are both often used in AJAX.[4] Even though JSON is named after JavaScript, it can be used in other programming languages, such as Python (PHP, etc.)[5]
Example
This is an example of JSON:
{
"firstName": "John",
"lastName" : "Smith",
"age" : 25,
"address" :
{
"streetAddress": "21 2nd Street",
"city" : "New York",
"state" : "NY",
"postalCode" : "10021"
},
"phoneNumber":
[
{
"type" : "",
"number": "212 555-1234"
},
{
"type" : "fax",
"number": "646 555-4567"
}
]
}
JSON Encoding and Decoding in Python
Encoding:
import json
sampleDict = {
"firstName": "John",
"lastName" : "Smith"
}
sampleJson = json.dumps(sampleDict, indent=4)
Decoding:
import json
# A JSON string or can be a JSON response
sampleJson = """{ "firstName": "John", "lastName" : "Smith"}"""
sampleDict = json.loads(sampleJson)
print(sampleDict['firstName'])
print(sampleDict['lastName'])
References
- ↑ "RFC 4627". Datatracker.ietf.org. 2011-10-26. Retrieved 2011-10-31.
- ↑ "What is the JSON format? | Data Basecamp". 2022-07-30. Retrieved 2022-08-11.
- ↑ 3.0 3.1 "JSON". JSON. Retrieved 2011-10-31.
- ↑ "JSON Beats XML, or Ajaj vs Ajax". Ajaxonomy. 2010-12-07. Archived from the original on 2011-10-14. Retrieved 2011-10-31.
- ↑ "18.2. json — JSON encoder and decoder — Python v2.7.2 documentation". Docs.python.org. Retrieved 2011-10-31.
Other websites
Content Disclaimer
Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.
- The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
- There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
- It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
- Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
- Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.