Index>Robot Kit>RaspArm-S Kit for RPi>Lesson 23 Reading and Saving json Files

Lesson 23 Reading and Saving json Files

1416

In this lesson, we will learn how to read and save json files.

23.1 Learning the code program of rwJson.py

Here we use Subline IDE to view and edit the code program of this course. For the specific method, please see "2.4 Editing the Code Program in Raspberry Pi" in Lesson 2.

In the file manager of the MobaXterm terminal, find adeept_rasparms/server, and open the code of this lesson: rwJson.py. The specific code and comments are as follows:

Import related dependent libraries.

image.png 


Get the absolute path of the .py file to find plan.json.

image.png 


Open plan.json.

image.png 


Read the content of plan.json.

image.png 


Decode the read content into the original format.

image.png 


At this point you can use planGoseList like a normal array.

image.png 


Convert the array to be saved into a json string.

image.png 


You can change the file name of newPlans.json and create a new file.

image.png 


Write json string in the newly created file.

image.png 


Save and close the newly created file.

You can find a file named newPlan.json appeared in the folder.

You can use the above method to import this file.

image.png 


23.2 Running rwJson.py program on Raspberry Pi

1. Open the terminal software MobaXterm:

image.png 


2. Log in to your Raspberry Pi (the way to log in to the Raspberry Pi has been introduced in Lesson 1):

image.png 


3. The relevant code programs of the RaspArm-S robot are stored in the folder of adeept_rasparms, which have been explained in "2.1 Downloading the code program to control the robot" in Lesson 2.First, you need to enter the server directory with the command window of the Raspberry Pi and enter the following command:

cd  adeept_rasparms/server

image.png 


4. Enter the command to view the contents of the current directory:

ls


image.png 


5.Enter the command to run the program:

sudo  python3  rwJson.py


image.png 


6.You can find a file named newPlan.json appears in the folder.

image.png