Movatterモバイル変換


[0]ホーム

URL:


Python Tutorial

Python json.decoder.JSONArray() Function



The Pythonjson.decoder.JSONArray() function is an internal helper function used to parse JSON arrays.

This function is primarily used by the JSON decoder when parsing array structures from a JSON-encoded string.

Syntax

Following is the syntax of the Pythonjson.decoder.JSONArray() function −

json.decoder.JSONArray(s_and_end, scan_once, **kwargs)

Parameters

This function accepts the following parameters −

  • s_and_end: A tuple containing the JSON string and the index position to start parsing.
  • scan_once: A function used for scanning JSON values.
  • **kwargs: Additional keyword arguments.

Return Value

This function returns a tuple containing the parsed list and the index position where parsing stopped.

Example: Basic Usage

In this example, we use thejson.decoder.JSONArray() function to parse a JSON array −

import json.decoder# JSON-encoded arrayjson_string = '[1, 2, 3]'# Define scan_once function properlyscan_once = json.decoder.JSONDecoder().scan_once# Parse JSON array using JSONArray()parsed_array, end_index = json.decoder.JSONArray((json_string, 1), scan_once)print("Parsed Array:", parsed_array)print("End Index:", end_index)

Following is the output of the above code −

Parsed Array: [1, 2, 3]End Index: 9

Example: Parsing Nested JSON Arrays

TheJSONArray() function can also handle nested JSON arrays −

import json.decoder# JSON-encoded nested arrayjson_string = '[[1, 2], [3, 4]]'# Get the scan_once function from JSONDecoderscan_once = json.decoder.JSONDecoder().scan_once# Parse JSON array using JSONArray()parsed_array, end_index = json.decoder.JSONArray((json_string, 1), scan_once)print("Parsed Nested Array:", parsed_array)print("End Index:", end_index)

Following is the output obtained −

Parsed Nested Array: [[1, 2], [3, 4]]End Index: 16

Example: Handling Empty JSON Arrays

The function correctly handles empty JSON arrays −

import json.decoder# JSON-encoded empty arrayjson_string = '[]'# Define scan_once functiondef scan_once(s, idx):   return json.decoder.scanstring(s, idx)# Parse JSON arrayparsed_array, end_index = json.decoder.JSONArray((json_string, 1), scan_once)print("Parsed Empty Array:", parsed_array)print("End Index:", end_index)

We get the output as shown below −

Parsed Empty Array: []End Index: 2

Example: Handling Invalid JSON Arrays

If the input is not a valid JSON array, aValueError is raised −

import json.decoder# Invalid JSON-encoded array (missing closing bracket)json_string = '[1, 2, 3'# Get the scan_once function from JSONDecoderscan_once = json.decoder.JSONDecoder().scan_oncetry:   # Attempt to parse an invalid JSON array   parsed_array, end_index = json.decoder.JSONArray((json_string, 1), scan_once)   print("Parsed Array:", parsed_array)except ValueError as e:   print("Error:", e)

The result produced is as follows −

Error: Expecting ',' delimiter: line 1 column 9 (char 8)
python_json.htm
Print Page
Advertisements

[8]ページ先頭

©2009-2025 Movatter.jp