CN115238655A - Json data editing method and device - Google Patents
Json data editing method and device Download PDFInfo
- Publication number
- CN115238655A CN115238655A CN202210832208.4A CN202210832208A CN115238655A CN 115238655 A CN115238655 A CN 115238655A CN 202210832208 A CN202210832208 A CN 202210832208A CN 115238655 A CN115238655 A CN 115238655A
- Authority
- CN
- China
- Prior art keywords
- node
- parameter
- json
- file
- editing
- Prior art date
- Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
- Pending
Links
- 238000000034 method Methods 0.000 title claims abstract description 60
- 238000013507 mapping Methods 0.000 claims abstract description 18
- 238000003780 insertion Methods 0.000 claims description 15
- 230000037431 insertion Effects 0.000 claims description 15
- 238000012217 deletion Methods 0.000 claims description 3
- 230000037430 deletion Effects 0.000 claims description 3
- 238000012986 modification Methods 0.000 description 12
- 230000004048 modification Effects 0.000 description 12
- 230000008569 process Effects 0.000 description 10
- 238000003491 array Methods 0.000 description 3
- 230000000007 visual effect Effects 0.000 description 3
- 238000004590 computer program Methods 0.000 description 2
- 238000010586 diagram Methods 0.000 description 2
- 238000004458 analytical method Methods 0.000 description 1
- 230000009286 beneficial effect Effects 0.000 description 1
- 238000013500 data storage Methods 0.000 description 1
- 230000000694 effects Effects 0.000 description 1
- 239000000835 fiber Substances 0.000 description 1
- 230000006870 function Effects 0.000 description 1
- 230000006872 improvement Effects 0.000 description 1
- 230000003287 optical effect Effects 0.000 description 1
- 239000004065 semiconductor Substances 0.000 description 1
- 239000007787 solid Substances 0.000 description 1
Images
Classifications
-
- G—PHYSICS
- G06—COMPUTING; CALCULATING OR COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F40/00—Handling natural language data
- G06F40/10—Text processing
- G06F40/166—Editing, e.g. inserting or deleting
-
- G—PHYSICS
- G06—COMPUTING; CALCULATING OR COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F40/00—Handling natural language data
- G06F40/10—Text processing
- G06F40/12—Use of codes for handling textual entities
- G06F40/14—Tree-structured documents
-
- G—PHYSICS
- G06—COMPUTING; CALCULATING OR COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F40/00—Handling natural language data
- G06F40/20—Natural language analysis
- G06F40/205—Parsing
Landscapes
- Engineering & Computer Science (AREA)
- Theoretical Computer Science (AREA)
- Health & Medical Sciences (AREA)
- Artificial Intelligence (AREA)
- Audiology, Speech & Language Pathology (AREA)
- Computational Linguistics (AREA)
- General Health & Medical Sciences (AREA)
- Physics & Mathematics (AREA)
- General Engineering & Computer Science (AREA)
- General Physics & Mathematics (AREA)
- Information Retrieval, Db Structures And Fs Structures Therefor (AREA)
Abstract
The invention relates to a json data editing method and a json data editing device, wherein the json data editing method comprises the following steps: receiving a first editing instruction aiming at a json data node in a command window, wherein the command window is positioned in a command interface of an operating system; acquiring an original json file corresponding to a json data node from a file system according to a path parameter in the first editing instruction, and reading the original json file into a memory; mapping an original json file into a tree structure in a memory, and determining that a json data node corresponds to a target node in the tree structure; and executing editing operation on the target node according to the operation parameters in the first editing instruction, and storing the edited file with the tree structure into a file with a json format in a file system to obtain a target json file. According to the scheme, the target nodes of the tree structure are modified in the memory, and grammar errors are not easy to generate based on the tree structure editing, so that a user does not need to spend time on json grammar formats, and the editing accuracy and efficiency are improved.
Description
Technical Field
The invention relates to the technical field of computers, in particular to a json data editing method and device.
Background
json is a lightweight data exchange format, and the compact and clear hierarchical structure makes json an ideal data exchange language. Viewing and editing json files is a frequent task in software development.
Currently, json files are generally edited and modified by text editors such as vi/emacs. However, the text editor does not parse the json file and does not recognize the json structural information, so that the correctness of the editing format needs to be manually checked for modifying the json file, which results in lower editing accuracy and lower editing efficiency of the json file.
Disclosure of Invention
The present invention is directed to solving at least one of the problems of the prior art. To this end, a first aspect of the present invention provides a json data editing method, including:
receiving a first editing instruction aiming at a json data node in a command window, wherein the command window is positioned in a command interface of an operating system;
acquiring an original json file corresponding to the json data node from a file system according to the path parameter in the first editing instruction, and reading the original json file into a memory;
mapping the original json file into a tree structure in the memory, and determining that the json data node corresponds to a target node in the tree structure;
according to the operation parameters in the first editing instruction, editing operation is executed on the target node in the tree structure;
and saving the edited file with the tree structure into a file with a json format in the file system.
Optionally, the obtaining an original json file corresponding to the json data node from a file system according to the path parameter in the first editing instruction includes:
determining the number of parameters contained in the first editing instruction;
if the first editing instruction comprises at least two parameters, acquiring a first parameter as a file name parameter and acquiring a second parameter as a path parameter;
and acquiring a target json file corresponding to the file name parameter from a file system according to the path parameter.
Optionally, after determining the number of parameters included in the first editing instruction, the method further includes:
if the first editing instruction comprises a parameter, acquiring the parameter as a file name parameter;
mapping the json file corresponding to the file name parameter into a tree structure in the memory;
displaying a console interface of a json editing tool and waiting to receive a first editing instruction for the json file in a command window.
Optionally, if the first editing instruction includes two parameters, after acquiring the target json file corresponding to the filename parameter from the file system, the method further includes:
and displaying the node information of the json file corresponding to the file name parameter and the path parameter.
Optionally, before obtaining the original json file corresponding to the json data node from the file system according to the path parameter in the first editing instruction, the method further includes:
and analyzing the path parameters in the first editing instruction.
Optionally, the analyzing the path parameter in the first editing instruction includes:
resolving the '/' in the path parameters into a root node;
analyzing the first parameter after the root node into the node name of the child node of the root node to obtain a first child node;
analyzing the first parameter behind the first child node into the node name of a subordinate node of the first child node to obtain a second child node;
if the parameter exists behind the second child node, analyzing the type of the second child node into a simple node, and analyzing the parameter into the value of the simple node;
and if the second child node has no parameter, analyzing the type of the second child node into an object node or an array node.
Optionally, the obtaining, according to the path parameter, a target json file corresponding to the filename parameter from a file system includes:
and acquiring a corresponding original json file from a file system according to the analyzed type information of the root node, the first child node, the second child node and the second child node in the path parameters.
Optionally, the executing, according to the operation parameter in the first editing instruction, an editing operation on the target node includes:
under the condition that the first editing instruction comprises at least three parameters, acquiring a third parameter as an operation parameter;
if the operation parameter is a target node deletion parameter, deleting the target node from the tree structure;
if the operation parameter is any one of a modified node name parameter and a modified node value parameter, acquiring a fourth parameter in the first editing instruction as a modified value, and replacing the node name or the node value of the target node with the modified value;
if the operation parameter is an insertion new node parameter, acquiring a fourth parameter, a fifth parameter and a sixth parameter in the first editing instruction, and respectively inserting the fifth parameter and the sixth parameter as a new node name and a new node value into an insertion path pointed by the fourth parameter.
Optionally, the method further includes:
receiving a second editing instruction aiming at a json data node in a command window, wherein the command window is positioned on a console interface of a json editing tool;
analyzing the operating parameters in the second editing instruction;
according to the operation parameters, editing operation is carried out on a current node in a tree structure, the current node is a node pointed by a default path in the console interface, and a json file corresponding to the current node is mapped into the tree structure in advance in a memory;
and storing the edited file with the tree structure into a file with a json format in a file system.
Optionally, the performing, according to the operation parameter, an editing operation on the current node in the tree structure includes:
if the operation parameter is any one of a parameter for modifying the name of the current node and a parameter for modifying the value of the current node, acquiring a first parameter in the first editing instruction as a modified value, and replacing the name or the value of the current node with the modified value;
if the operation parameter is the parameter of deleting the current node, deleting the current node from the tree structure;
if the operation parameter is a new node insertion parameter, acquiring a first parameter, a second parameter and a third parameter in the first editing instruction, and respectively inserting the second parameter and the third parameter as a new node name and a new node value into an insertion path pointed by the first parameter;
if the operation parameter is a switching path parameter, acquiring a first parameter in the first editing instruction as a new path, and switching a current node into a node pointed by the new path;
if the operation parameter is a viewing path parameter, printing the path information of the current node;
if the operation parameters are storage parameters, storing the json file in the current memory into a file system;
and if the operation parameter is an exit parameter, exiting the json editing tool.
A second aspect of the present invention provides a json data editing apparatus, including:
the system comprises a first editing instruction receiving module, a second editing instruction receiving module and a third editing instruction receiving module, wherein the first editing instruction receiving module is used for receiving a first editing instruction aiming at a json data node in a command window, and the command window is positioned in a command interface of an operating system;
the first original file acquisition module is used for acquiring an original json file corresponding to the json data node from a file system according to the path parameter in the first editing instruction and reading the original json file into a memory;
the first mapping module is used for mapping the original json file into a tree structure in the memory and determining that the json data node corresponds to a target node in the tree structure;
the first editing module is used for executing editing operation on the target node in the tree structure according to the operation parameters in the first editing instruction;
and the first storage module is used for storing the edited file with the tree structure into a file with a json format in the file system.
Optionally, the first original file acquiring module is specifically configured to:
determining the number of parameters contained in the first editing instruction;
if the first editing instruction comprises at least two parameters, acquiring a first parameter as a file name parameter and acquiring a second parameter as a path parameter;
and acquiring a target json file corresponding to the file name parameter from a file system according to the path parameter.
Optionally, the apparatus further comprises:
the file name parameter acquisition module is used for acquiring a parameter as a file name parameter if the first editing instruction contains the parameter;
the second mapping module is used for mapping the json file corresponding to the file name parameter into a tree structure in the memory;
and the console interface display module is used for displaying a console interface of the json editing tool and waiting for receiving a first editing instruction aiming at the json file in a command window.
Optionally, the apparatus further comprises:
and the node information display module is used for displaying the file name parameters and the node information of the json file corresponding to the path parameters.
Optionally, the apparatus further comprises:
and the path parameter analysis module is used for analyzing the path parameters in the first editing instruction.
Optionally, the path parameter analyzing module is specifically configured to:
resolving the '/' in the path parameters into a root node;
analyzing the first parameter behind the root node into the node name of the child node of the root node to obtain a first child node;
analyzing the first parameter behind the first child node into the node name of a subordinate node of the first child node to obtain a second child node;
if the parameter exists behind the second child node, analyzing the type of the second child node into a simple node, and analyzing the parameter into the value of the simple node;
and if the second child node has no parameter, analyzing the type of the second child node into an object node or an array node.
Optionally, the first original file obtaining module is specifically configured to:
and acquiring a corresponding original json file from a file system according to the analyzed type information of the root node, the first child node, the second child node and the second child node in the path parameters.
Optionally, the first editing module is specifically configured to:
under the condition that the first editing instruction comprises at least three parameters, acquiring a third parameter as an operation parameter;
if the operation parameter is a target node deletion parameter, deleting the target node from the tree structure;
if the operation parameter is any one of a modified node name parameter and a modified node value parameter, acquiring a fourth parameter in the first editing instruction as a modified value, and replacing the node name or the node value of the target node with the modified value;
if the operation parameter is a new node insertion parameter, acquiring a fourth parameter, a fifth parameter and a sixth parameter in the first editing instruction, and respectively inserting the fifth parameter and the sixth parameter as a new node name and a new node value into an insertion path pointed by the fourth parameter.
Optionally, the apparatus further comprises:
the second editing instruction receiving module is used for receiving a second editing instruction aiming at the json data node in a command window, and the command window is positioned on a console interface of a json editing tool;
the operating parameter analyzing module is used for analyzing the operating parameters in the second editing instruction;
a second editing module, configured to execute an editing operation on a current node in a tree structure according to the operation parameter, where the current node is a node pointed by a default path in the console interface, and a json file corresponding to the current node is mapped in advance in a memory as the tree structure;
and the second storage module is used for storing the edited file with the tree structure into a file with a json format in a file system.
Optionally, the second editing module is specifically configured to:
if the operation parameter is any one of a parameter for modifying the name of the current node and a parameter for modifying the value of the current node, acquiring a first parameter in the first editing instruction as a modified value, and replacing the name or the value of the current node with the modified value;
if the operation parameter is the parameter of deleting the current node, deleting the current node from the tree structure;
if the operation parameter is a new node insertion parameter, acquiring a first parameter, a second parameter and a third parameter in the first editing instruction, and respectively inserting the second parameter and the third parameter as a new node name and a new node value into an insertion path pointed by the first parameter;
if the operation parameter is a switching path parameter, acquiring a first parameter in the first editing instruction as a new path, and switching a current node to a node pointed by the new path;
if the operation parameter is a viewing path parameter, printing the path information of the current node;
if the operation parameters are storage parameters, storing the json file in the current memory into a file system;
and if the operation parameter is an exit parameter, exiting the json editing tool.
A third aspect of the present invention provides an electronic device, which includes a processor and a memory, where at least one instruction, at least one program, a set of codes, or a set of instructions is stored, and the at least one instruction, the at least one program, the set of codes, or the set of instructions is loaded and executed by the processor to implement the json data editing method according to the first aspect.
A fourth aspect of the present invention provides a computer-readable storage medium having stored therein at least one instruction, at least one program, set of codes, or set of instructions, which is loaded and executed by a processor to implement the json data editing method according to the first aspect.
The embodiment of the invention has the following beneficial effects:
in the embodiment of the invention, a first editing instruction aiming at a json data node is received in a command window, and the command window is positioned in a command interface of an operating system; acquiring an original json file corresponding to the json data node from a file system according to the path parameter in the first editing instruction, and reading the original json file into a memory; mapping the original json file into a tree structure in the memory, and determining that the json data node corresponds to a target node in the tree structure; and executing editing operation on the target node according to the operation parameters in the first editing instruction, and storing the edited file with the tree structure as a file in a json format in the file system to obtain a target json file. According to the scheme, the first editing instruction is received in the command interface, the path parameter and the operation parameter in the first editing instruction are analyzed and recognized, the target node of the tree structure is modified in the memory, and grammar errors are not easy to generate based on tree structure editing, so that a user does not need to spend time on a json grammar format, and the editing accuracy and efficiency are improved.
Additional aspects and advantages of the invention will be set forth in part in the description which follows and, in part, will be obvious from the description, or may be learned by practice of the invention.
Drawings
In order to more clearly illustrate the technical solution of the present invention, the drawings used in the description of the embodiment or the prior art will be briefly described below. It is obvious that the drawings in the following description are only some embodiments of the invention, and that for a person skilled in the art it is also possible to derive other drawings from them without inventive effort.
Fig. 1 is a flowchart illustrating steps of a first json data editing method according to an embodiment of the present invention;
fig. 2 is a flowchart illustrating steps of a second json data editing method according to an embodiment of the present invention;
fig. 3 is a flowchart illustrating steps of a third json data editing method according to an embodiment of the present invention;
fig. 4 is a flowchart of an operation of editing a json file by using a command mode according to an embodiment of the present invention;
fig. 5 is a flowchart illustrating an operation of json file editing in the console mode according to an embodiment of the present invention;
fig. 6 is a block diagram of a json data editing apparatus according to an embodiment of the present invention.
Detailed Description
The technical solutions in the embodiments of the present invention will be clearly and completely described below with reference to the drawings in the embodiments of the present invention, and it is obvious that the described embodiments are only a part of the embodiments of the present invention, and not all of the embodiments. All other embodiments, which can be obtained by a person skilled in the art without any inventive step based on the embodiments of the present invention, are within the scope of the present invention.
The present specification provides for editing steps of the method as described in the examples or flowcharts, but may include more or fewer editing steps based on routine or non-invasive labor. In practice, the system or server product may be implemented in a sequential or parallel manner (e.g., parallel processor or multi-threaded environment) according to the embodiments or methods shown in the figures.
Fig. 1 is a flowchart illustrating steps of a first json data editing method according to an embodiment of the present invention. The method may comprise the steps of:
In the embodiment of the invention, the command window is located in a command interface of an operating system, such as a shell interface of a linux system, a cmd interface of a windows system and the like.
The user inputs a first editing instruction for the json data node in a command window, and the format of the first editing instruction can be file name/path parameter/operation value.
The traditional json file editing tool is generally a text editing tool or a visual editing tool, and cannot be applied to editing json files under a command line interface. The software tool developed by the scheme can enable a user to directly input the first editing instruction in the command window of the operating system, is simpler and more efficient, and improves the json file editing efficiency of the user.
And 102, acquiring an original json file corresponding to the json data node from a file system according to the path parameter in the first editing instruction, and reading the original json file into a memory.
Specifically, the format of the json first editing instruction of the command mode in the scheme is as follows:
< JSON file name > < JSON node name > [ modify node name/modify node value/delete node/insert node ] [ node name/node value ].
Wherein < JSON file name > < JSON node name > is a path parameter, and [ modify node name/modify node value/delete node/insert node ] [ node name/node value ] is an operation parameter.
And acquiring a json file to which the json data node to be edited belongs from the position to obtain an original json file.
Reading the original json file into a memory, and operating a program from the memory to edit the json file.
The Json file is in a hierarchical structure, a third-party fast Json library is called, and the original Json file is analyzed to obtain information of a root node and each child node. And mapping the root node and each sub node in the original json file into a tree structure, further determining the position of the json data node to be edited corresponding to the node in the tree structure, and further mapping the json data to be edited into a target node in the tree structure.
And 104, executing editing operation on the target node according to the operating parameters in the first editing instruction.
The operating parameters are in the form of strings, with different strings representing different operating parameters. Specifically, the operation parameters include operation parameters corresponding to operations such as adding, deleting, modifying, and viewing.
And executing editing operation on the target node, wherein the editing operation comprises the operations of adding, deleting, modifying, viewing and the like on the target node, and the editing operation is executed aiming at the tree structure in the memory, namely the editing operation takes effect on the tree structure.
And 105, storing the edited file with the tree structure into a file with a json format in the file system.
After the editing is finished, all editing results are stored in a json format, and all editing operations can be finished.
Traditional JSON text editors do not parse JSON files nor recognize JSON content. The software tool provided by the scheme can analyze and recognize the JSON content structure, form a tree structure of nodes in the memory, and map JSON data nodes to the nodes of the tree structure in the memory.
The scheme is used for editing and modifying JSON data, namely modifying nodes of a tree structure in a memory, and exporting the tree structure in the memory into a JSON format file after modification, so that a user does not need to spend time on a syntax format of JSON and syntax errors caused by modification can be avoided.
In addition, the JSON is edited and modified through the commands of the tool, so that a plurality of commands can be combined by using the command script to carry out automatic editing operation, manual intervention is reduced, and the editing accuracy and the editing efficiency are further improved.
In summary, in the embodiment of the present invention, a first editing instruction for a json data node is received in a command window, where the command window is located in a command interface of an operating system; acquiring an original json file corresponding to the json data node from a file system according to the path parameter in the first editing instruction, and reading the original json file into a memory; mapping the original json file into a tree structure in the memory, and determining that the json data node corresponds to a target node in the tree structure; according to the operation parameters in the first editing instruction, editing operation is executed on the target node in the tree structure; and saving the edited file with the tree structure into a file with a json format in the file system. According to the scheme, the first editing instruction is received in the command interface, the path parameter and the operation parameter in the first editing instruction are analyzed and recognized, the target node of the tree structure is modified in the memory, and grammar errors are not easy to generate based on tree structure editing, so that a user does not need to spend time on a json grammar format, and the editing accuracy and efficiency are improved.
Fig. 2 is a flowchart illustrating steps of a second json data editing method according to an embodiment of the present invention. The method may comprise the steps of:
In the embodiment of the present invention, step 201 may refer to step 101, which is not described herein again.
And step 202, analyzing the path parameters in the first editing instruction.
The path parameters in this scheme are divided by "/" from the root path to the child node names, for example: grandfather node/current node. By analyzing the path parameters, the data of the root node and each layer of child nodes can be obtained, so that path positioning is convenient.
In one possible embodiment, step 202 includes steps 2021-2025:
step 2021, resolving the "/" in the path parameters into root nodes;
step 2022, resolving the first parameter after the root node into a node name of a child node of the root node, so as to obtain a first child node;
step 2023, resolving the first parameter after the first child node into a node name of a subordinate node of the first child node, so as to obtain a second child node;
step 2024, if there is a parameter behind the second child node, analyzing the type of the second child node as the simple node, and analyzing the parameter as the value of the simple node;
step 2025, if there is no parameter behind the second child node, parsing the type of the second child node into the object node or the array node.
In steps 2021 to 2025, if the node is the root node of the tree structure, the node name is fixed to "/".
The first parameter after the root node is the node name of the first child node under the root node, and the first parameter after the first child node is the node name of the child node under the first child node and is the second child node. If the parameter exists behind the second child node, the second child node is a simple node, and the parameter is analyzed to be the value of the simple node.
And if the node belongs to a child node of the object node, the node name is the key value of the corresponding JSON data; if the node belongs to a child node of the array node, the node name is the index value of the corresponding JSON data.
In the embodiment of the present invention, for example, if the path parameter is "/menu/pop/menu", the step of obtaining the corresponding original json file from the path parameter is as follows:
first, a root node is found. The first character "/" is taken from the path parameter "/menu/pop/menu" to get the root node.
Then, the corresponding child node under the root node is found. Specifically, the first word after "/" is taken to be "menu", that is, a child node named "menu" under the root node is to be found. And then sequentially matching child nodes below the root node in the memory according to the 'menu', if the matching is successful, indicating that the corresponding child nodes are found, and if all the child nodes are not successfully matched, reporting that the searching is failed and quitting the command.
And finally, continuing to search for location until the end of the path parameter word. Specifically, the first word behind the 'menu' is found to be 'pop' (note: the '/' between the 'menu' and the 'pop' is identified as a separator, and is not the meaning of a word nor a root node), then child nodes below the root node are sequentially matched in the memory according to the 'pop', if matching is successful, the corresponding child nodes are found, and if all child nodes are not successfully matched, the search failure is reported and the command is quitted. If the path character string is not finished, namely, words are left behind the 'popup', the recursive search of child nodes is continued, if the path character string is finished, the finally positioned node is the node designated by the path, and the positioning of the node is finished according to the path.
And 204, the original json file comprises three data formats of a simple value, an object and an array, the simple value is mapped into a simple node of the tree structure, the object is mapped into an object node of the tree structure, the array is mapped into an array node of the tree structure, and the json data node is determined to correspond to a target node in the tree structure.
In the embodiment of the invention, JSON has three data types: simple values, objects and arrays. The simple value refers to data of types such as character strings, numerical values, boolean values, null and the like, the object is a complex data type, and the object is represented as an ordered key-value pair. Each value may be a simple value or may be a complex type. An array is a complex data type that represents an ordered list of values that can be accessed through numerical indices. The values of an array may be of any type, including simple values, objects, and even other arrays.
Specifically, the data types in the json file are analyzed, and the simple values, the objects and the arrays are mapped into simple nodes, object nodes and array nodes in the tree structure respectively.
In addition, a data node to be edited is obtained from the first editing instruction, and then a node position in the tree structure corresponding to the data node is determined to obtain a target node.
And step 205, determining the number of parameters contained in the first editing instruction.
Because every two parameters are separated by a '/', the number of the parameters can be determined according to the number of the '/'.
After step 205, according to the number of parameters contained in the first editing instruction, step 206 to step 211, or step 212, or step 213 to step 215 can be selected to be executed.
Step 206, if the first editing instruction includes at least two parameters, acquiring a first parameter as a file name parameter and a second parameter as a path parameter;
and step 207, acquiring a target json file corresponding to the file name parameter from a file system according to the path parameter.
In steps 206-207, if the first editing instruction includes at least two parameters, the first two parameters are taken, the first parameter is used as a file name, the second parameter is used as a path, and a corresponding target json file interface is obtained from the file system according to the file name and the path.
After step 207 is executed, step 208 to step 211 can be further selected and executed according to the number of parameters and the content of the operation parameters included in the first editing instruction.
Step 208, acquiring a third parameter as an operation parameter under the condition that the first editing instruction comprises at least three parameters;
step 210, if the operation parameter is any one of a modified node name parameter and a modified node value parameter, acquiring a fourth parameter in the first editing instruction as a modified value, and replacing the node name or the node value of the target node with the modified value;
step 211, if the operation parameter is an insert new node parameter, acquiring a fourth parameter, a fifth parameter, and a sixth parameter in the first editing instruction, and inserting the fifth parameter and the sixth parameter as a new node name and a new node value into an insert path pointed by the fourth parameter, respectively.
In steps 208-211, if the first editing instruction includes three or more parameters, the third parameter is an operation parameter. And selecting and executing the steps 209, 210 and 211 according to the content of the operation parameters.
In step 209, if the target node is a simple node, the simple node is deleted from the tree structure, and if the target node is a non-simple node, all child nodes under the target node are deleted simultaneously.
If the target node is a child node of the object node, the operation of deleting the target node is as follows:
for a json data node: { "key1": value 1"," key2":123}, and the path of the target node is"/key 2". When the command is executed, the corresponding target node is positioned according to the path, namely 'key 2': 123. After the target node is deleted, the result is { "key1": a "value of 1" }.
If the target node is a child node of the array node, the operation of deleting the target node is as follows:
for a json data node: [ "string",123, true,456.789], the path of the target node is "/2". When the command is executed, the corresponding target node is positioned according to the path to be 'true'. After the target node is deleted, the result is [ "string",123,456.789].
In step 210, if the operation parameter is the modified node name parameter, the target node name is replaced with a new node name.
If the target node is a child node of the object node, a certain key value of json object data is modified. The operations for modifying the node name are exemplified as follows:
for json data nodes: { "key1": value 1"," key2":123}, the path of the target node is"/key 2", and the new node name is" key3". When the command is executed, the corresponding target node is positioned according to the path to be ' key2 ': 123, and after the node name ' key2 ' is modified to be ' key3 ', the result is { ' key1 ': value 1 ', ' key3 ': 123}.
If the target node is a child node of the array node, the modified target node is the index of a certain element of the json array data. An example of the operation of modifying the node name is as follows:
for a json data node: [ "string",123, true,456.789], the path of the target node is "/2", and the new node name is "3". When the command is executed, the corresponding node is located to be true according to the path, and after the node name of "2" is modified to be "3", the result is [ "string",123,456, 789, true ].
And if the operation parameter is the modified node value parameter, replacing the target node value with a new node value.
If the target node is a child node of the object node, the operation of modifying the node value is as follows:
for a json data node: { "key1": value 1"," key2":123}, the path of the target node is"/key 2", and the new node value is" 321". When the command is executed, the corresponding node is positioned according to the path, wherein the path is ' key2 ': 123, and after the node value 123 is modified to 321, the result is { ' key1 ': value 1 ', ' key2 ': 321}.
If the target node is a child node of the array node, the operation of modifying the node value is as follows:
for a json data node: [ "string",123, true,456.789], the path of the target node is "/2", and the new node value is "false". When the command is executed, firstly, the path is positioned to the corresponding node according to the path and is "/2", the corresponding node value is "true", and after the node value "true" is modified to "false", the result is [ "string",123, false,456.789].
In step 211, three parameters, i.e., a fourth parameter, a fifth parameter, and a sixth parameter, are required to insert a new node. The fifth parameter is a new node name, and the sixth parameter is a new node value.
Specifically, a new node position is located according to the path, and if the located new node position does not exist, an error is reported back. And then, creating a new node object in the memory according to the new node name and the new node value, and finally inserting the newly created node into the position specified by the path.
If the inserted new node value is an object node, only an empty object can be obtained, and the new node value is represented by '{ }'; the inserted new node value can only be an empty array if it is an array node, and is represented by "[ ]".
If the new node to be inserted is a child node of the object node, the operation of inserting the new node is as follows:
for json data nodes: { "key1": value 1"," key2":123}, new node path is"/", new node name is" key3", new node value is 456. When the command is executed, the command is firstly positioned according to the path until the corresponding parent node is "/", the node to be inserted is "key3":456, and after the command is inserted, the result is { "key1": value 1"," key2": 123", and "key3":456}.
If the new node to be inserted is a child node of the array node, the operation of inserting the new node is as follows:
for a json data node: [ "string",123, true,456.789], the new node path is "/", the new node name is "2", and the new node value is false. When the command is executed, firstly, the corresponding parent node is positioned according to the path, namely the path is/, the node to be inserted is 2: false. After insertion, the result is [ "string",123, false, true,456.789].
And 212, if the first editing instruction comprises two parameters, displaying node information of the json file corresponding to the file name parameter and the path parameter.
Specifically, if the first editing instruction only includes two parameters, that is, only includes a file name parameter and a path parameter, the first editing instruction is defaulted as a viewing instruction, and node information of a json file corresponding to the file name parameter and the path parameter is displayed in a command window.
Step 213, if the first editing instruction includes a parameter, acquiring the parameter as a file name parameter;
step 214, mapping the json file corresponding to the file name parameter into a tree structure in the memory;
In steps 213 to 215, if the first editing instruction only includes one parameter, the parameter is used as a file name parameter, and the json file corresponding to the file name is mapped into a tree structure in the memory. The console interface of the json editing tool is then displayed. The console interface is a visual json editing interface and comprises a command window, and a first editing instruction aiming at json files can be received.
And step 216, storing the edited file with the tree structure into a file with a json format in the file system.
In the embodiment of the present invention, step 216 may refer to step 105, which is not described herein again.
The json data editing method in fig. 2 provides a specific method for editing json files in a command interface of an operating system. The json file can be processed in batch and automatically by adopting a command mode, a plurality of json first editing instructions are placed in one batch script, the batch script can be executed to automatically process the json file, and the json file can be conveniently and automatically processed by a program.
Fig. 3 is a flowchart illustrating steps of a third json data editing method according to an embodiment of the present invention. The method may comprise the steps of:
and step 304, storing the edited file with the tree structure into a file with a json format in a file system.
In the embodiment of the invention, the console interface is a visual json editing interface, and comprises a command window which can receive a second editing instruction aiming at a json file.
The json data editing method in fig. 3 is a method of editing a json file in a console mode. The console mode is that a JSON editing tool is started first, then whether a command is input in a command window of the console provided by the JSON editing tool is detected in a circulating mode, if the command is input, a current JSON file is loaded, and a tree model of nodes is formed in a memory. Then various editing or viewing commands are executed in the console, all modifications are effective on the model in the memory, and finally all modification operations can be completed by exporting all modification results to the JSON file through the saving command.
The default path parameter in the console mode is a path parameter of a json file edited last time, so that the first and second editing instructions of the console mode do not include the path parameter, and the command format of the console mode is as follows: [ first parameter ] [ second parameter ]. Wherein the first parameter is an operating parameter and the second parameter is an operating value. If the json file is to be changed, the first and second editing instructions for switching the path may be executed.
The console mode is suitable for the condition that the JSON files are edited frequently, and can be repeatedly modified in the console mode until the modification is completed and then the saving is finished.
Except that the command window of the console receives the first and second editing instructions, and the command format is different from that of the command mode in fig. 1 and fig. 2, the process of editing the json data node in the console mode is the same as the command mode, and the specific editing process may refer to the command mode operation method described above, and details are not repeated herein.
In a possible implementation manner, the performing an editing operation on the current node according to the operation parameter includes:
if the operation parameter is any one of a parameter for modifying the name of the current node and a parameter for modifying the value of the current node, acquiring a first parameter in the first and second editing instructions as a modified value, and replacing the name or the value of the current node with the modified value;
if the operation parameter is the parameter of deleting the current node, deleting the current node from the tree structure;
if the operation parameter is a new node insertion parameter, acquiring a first parameter, a second parameter and a third parameter in the first and second editing instructions, and respectively inserting the second parameter and the third parameter as a new node name and a new node value into an insertion path pointed by the first parameter;
if the operation parameter is a switching path parameter, acquiring a first parameter in the first and second editing instructions as a new path, and switching a current node into a node pointed by the new path;
if the operation parameter is a checking path parameter, printing the path information of the current node;
if the operation parameters are storage parameters, storing the json file in the current memory into a file system;
and if the operation parameter is an exit parameter, exiting the json editing tool.
In the embodiment of the present invention, the modifying operation, the deleting operation, and the inserting operation may refer to step 209 to step 211 in fig. 2, which is not described herein again.
If the operation parameter is a switching path parameter, the first parameter in the first editing instruction and the second editing instruction can be used as a new path, and the current node is switched to the node pointed by the new path, so that path switching of the json file is completed, and the node pointed by the new path can be directly edited subsequently.
In addition, the console mode also comprises a saving command, and the modified json file can be directly saved by using the saving parameters. The console mode also includes an exit command, and the json editing tool can be directly exited using an exit parameter.
Fig. 4 is a flowchart of an operation of editing a json file by using a command mode according to an embodiment of the present invention. The command mode is an editing mode in which the command window is located in a command interface of the operating system.
As shown in fig. 4, it is first judged whether there is a parameter in the first edit instruction, and if there is no tape parameter, the printing of the help information is ended.
If one parameter is carried, the first parameter is taken as the name of the JSON file, the corresponding JSON file is read and analyzed to generate a tree model in the memory, and then the mode of the console is entered.
And if the two parameters are carried, taking the second parameter as a node path, positioning to the corresponding JSON node, and then printing the node information.
If three parameters are included, it is determined what the third parameter is, specifically as follows:
if the third parameter is the command for modifying the node name, taking the fourth parameter as the new node name, and executing the command for modifying the node name to finish;
if the third parameter is the 'node value modification command', taking the fourth parameter as a new node value, and ending the 'node value modification command';
if the third parameter is a command of deleting the current node, the command of deleting the current node is executed, and the operation is finished;
if the third parameter is the 'insert node command', taking the fourth parameter as a new node name, taking the fifth parameter as a new node value, and then executing the 'insert node command' to end;
if the third parameter is an unrecognizable command, an unrecognizable command is reported and the process exits.
Fig. 5 is a flowchart of an operation of editing a json file in a console mode according to an embodiment of the present invention. The console mode is an editing mode in which the command window is located on a console interface of the json editing tool.
As shown in fig. 5, the json editing tool detects whether a command is input to the console in a loop, and if the command is input, determines the command and specifically executes the following scheme:
if the command is 'modify node name', the 1 st parameter is taken as the new name of the node, the 'modify node name command' is executed, and the state of waiting for the input command is returned.
If the node value modification command is 'the node value modification command', the 1 st parameter is taken as the value of the node, the 'node value modification command' is executed, and the state of waiting for the input command is returned.
If the command of deleting the current node is available, deleting the current node positioned by the path and returning to the state of waiting for the input command.
If the command is 'insert node command', taking the 1 st parameter as the name of the child node and the 2 nd parameter as the value of the child node, inserting a new node, and returning to the state of waiting for the input command.
If the command is 'switching path command', taking the 1 st parameter as a JSON path, positioning to a corresponding JSON node, and returning to a state waiting for inputting the command.
If the path command is checked, the path information of the current node is printed, and the state of waiting for the input command is returned.
If the command is saved, saving the JSON data in the memory into a JSON file, and returning to the state of waiting for the input command.
If the command is 'exit command', the console mode is exited, and the program is ended.
If the command is unrecognizable, the command is reported by mistake, and then the state of waiting for inputting the command is returned.
Fig. 6 is a block diagram of a json data editing apparatus according to an embodiment of the present invention. The apparatus 400 comprises:
a first editing instruction receiving module 401, configured to receive a first editing instruction for a json data node in a command window, where the command window is located in a command interface of an operating system;
a first original file obtaining module 402, configured to obtain an original json file corresponding to the json data node from a file system according to the path parameter in the first editing instruction, and read the original json file into a memory;
a first mapping module 403, configured to map the original json file into a tree structure in the memory, and determine that the json data node corresponds to a target node in the tree structure;
a first editing module 404, configured to perform an editing operation on the target node in the tree structure according to an operation parameter in the first editing instruction;
a first saving module 405, configured to save the edited file with the tree structure in the file system as a file in json format.
It can be clearly understood by those skilled in the art that, for convenience and simplicity of description, the specific working processes of the above-described systems, apparatuses and units may refer to the corresponding processes in the foregoing method embodiments, and are not described herein again.
In yet another embodiment provided by the present invention, an apparatus is further provided, which includes a processor and a memory, where the memory stores at least one instruction, at least one program, a set of codes, or a set of instructions, and the at least one instruction, the at least one program, the set of codes, or the set of instructions is loaded and executed by the processor to implement the json data editing method described in the embodiment of the present invention.
In yet another embodiment provided by the present invention, a computer-readable storage medium is further provided, in which at least one instruction, at least one program, code set, or instruction set is stored, and the at least one instruction, the at least one program, the code set, or the instruction set is loaded and executed by a processor to implement the json data editing method described in the embodiment of the present invention.
In the above embodiments, all or part of the implementation may be realized by software, hardware, firmware, or any combination thereof. When implemented in software, may be implemented in whole or in part in the form of a computer program product. The computer program product includes one or more computer instructions. When loaded and executed on a computer, cause the processes or functions described in accordance with the embodiments of the invention to occur, in whole or in part. The computer may be a general purpose computer, a special purpose computer, a network of computers, or other programmable device. The computer instructions may be stored in a computer readable storage medium or transmitted from one computer readable storage medium to another, for example, from one website site, computer, server, or data center to another website site, computer, server, or data center via wired (e.g., coaxial cable, fiber optic, digital Subscriber Line (DSL)) or wireless (e.g., infrared, wireless, microwave, etc.). The computer-readable storage medium can be any available medium that can be accessed by a computer or a data storage device, such as a server, a data center, etc., that includes one or more available media. The usable medium may be a magnetic medium (e.g., floppy Disk, hard Disk, magnetic tape), an optical medium (e.g., DVD), or a semiconductor medium (e.g., solid State Disk (SSD)), among others.
It is noted that, herein, relational terms such as first and second, and the like are used solely to distinguish one entity or edit from another entity or edit without necessarily requiring or implying any actual such relationship or order between such entities or edits. Also, the terms "comprises," "comprising," or any other variation thereof, are intended to cover a non-exclusive inclusion, such that a process, method, article, or apparatus that comprises a list of elements does not include only those elements but may include other elements not expressly listed or inherent to such process, method, article, or apparatus. Without further limitation, an element defined by the phrases "comprising a," "8230," "8230," or "comprising" does not exclude the presence of additional like elements in a process, method, article, or apparatus that comprises the element.
All the embodiments in the present specification are described in a related manner, and the same and similar parts among the embodiments may be referred to each other, and each embodiment focuses on differences from other embodiments. In particular, for the system embodiment, since it is substantially similar to the method embodiment, the description is simple, and for the relevant points, reference may be made to the partial description of the method embodiment.
The above description is only for the preferred embodiment of the present invention, and is not intended to limit the scope of the present invention. Any modification, equivalent replacement, or improvement made within the spirit and principle of the present invention shall fall within the protection scope of the present invention.
Claims (13)
1. A json data editing method, the method comprising:
receiving a first editing instruction aiming at a json data node in a command window, wherein the command window is positioned in a command interface of an operating system;
acquiring an original json file corresponding to the json data node from a file system according to the path parameter in the first editing instruction, and reading the original json file into a memory;
mapping the original json file into a tree structure in the memory, and determining that the json data node corresponds to a target node in the tree structure;
executing editing operation on the target node according to the operating parameters in the first editing instruction;
and storing the edited file with the tree structure into a file with a json format in the file system.
2. The method according to claim 1, wherein the obtaining an original json file corresponding to the json data node from a file system according to the path parameter in the first editing instruction comprises:
determining the number of parameters contained in the first editing instruction;
if the first editing instruction comprises at least two parameters, acquiring a first parameter as a file name parameter and acquiring a second parameter as a path parameter;
and acquiring a target json file corresponding to the file name parameter from a file system according to the path parameter.
3. The method according to claim 2, further comprising, after determining the number of parameters included in the first editing instruction:
if the first editing instruction comprises a parameter, acquiring the parameter as a file name parameter;
mapping the json file corresponding to the file name parameter into a tree structure in the memory;
displaying a console interface of a json editing tool and waiting to receive a first editing instruction for the json file in a command window.
4. The method according to claim 2, wherein if the first editing instruction includes two parameters, after acquiring the target json file corresponding to the filename parameter from the file system, the method further includes:
and displaying the node information of the json file corresponding to the file name parameter and the path parameter.
5. The method according to claim 2, further comprising, before obtaining an original json file corresponding to the json data node from a file system according to the path parameter in the first editing instruction:
and analyzing the path parameters in the first editing instruction.
6. The method of claim 5, wherein the parsing the path parameters in the first editing instruction comprises:
resolving the '/' in the path parameters into a root node;
analyzing the first parameter behind the root node into the node name of the child node of the root node to obtain a first child node;
analyzing the first parameter after the first child node into the node name of the subordinate node of the first child node to obtain a second child node;
if the parameter exists behind the second child node, analyzing the type of the second child node into a simple node, and analyzing the parameter into the value of the simple node;
and if the second child node has no parameter, analyzing the type of the second child node into an object node or an array node.
7. The method according to claim 6, wherein the obtaining a target json file corresponding to the filename parameter from a file system according to the path parameter comprises:
and acquiring a corresponding original json file from a file system according to the analyzed type information of the root node, the first child node, the second child node and the second child node in the path parameters.
8. The method of claim 2, wherein performing an editing operation on the target node according to the operation parameters in the first editing instruction comprises:
under the condition that the first editing instruction comprises at least three parameters, acquiring a third parameter as an operation parameter;
if the operation parameter is a target node deletion parameter, deleting the target node from the tree structure;
if the operation parameter is any one of a modified node name parameter and a modified node value parameter, acquiring a fourth parameter in the first editing instruction as a modified value, and replacing the node name or the node value of the target node with the modified value;
if the operation parameter is a new node insertion parameter, acquiring a fourth parameter, a fifth parameter and a sixth parameter in the first editing instruction, and respectively inserting the fifth parameter and the sixth parameter as a new node name and a new node value into an insertion path pointed by the fourth parameter.
9. The method of claim 1, further comprising:
receiving a second editing instruction aiming at a json data node in a command window, wherein the command window is positioned on a console interface of a json editing tool;
analyzing the operating parameters in the second editing instruction;
according to the operation parameters, editing operation is carried out on a current node in a tree structure, the current node is a node pointed by a default path in the console interface, and a json file corresponding to the current node is mapped into the tree structure in advance in a memory;
and saving the edited file with the tree structure into a file with a json format in a file system.
10. The method according to claim 9, wherein said performing an editing operation on the current node in the tree structure according to the operation parameter comprises:
if the operation parameter is any one of a parameter for modifying the name of the current node and a parameter for modifying the value of the current node, acquiring a first parameter in the first editing instruction as a modified value, and replacing the name or the value of the current node with the modified value;
if the operation parameter is the parameter for deleting the current node, deleting the current node from the tree structure;
if the operation parameter is a new node insertion parameter, acquiring a first parameter, a second parameter and a third parameter in the first editing instruction, and respectively inserting the second parameter and the third parameter as a new node name and a new node value into an insertion path pointed by the first parameter;
if the operation parameter is a switching path parameter, acquiring a first parameter in the first editing instruction as a new path, and switching a current node into a node pointed by the new path;
if the operation parameter is a checking path parameter, printing the path information of the current node;
if the operation parameters are storage parameters, storing the json file in the current memory into a file system;
and if the operation parameter is an exit parameter, exiting the json editing tool.
11. A json data editing apparatus, comprising:
the first editing instruction receiving module is used for receiving a first editing instruction aiming at a json data node in a command window, and the command window is positioned on a command interface of an operating system;
a first original file obtaining module, configured to obtain an original json file corresponding to the json data node from a file system according to the path parameter in the first editing instruction, and read the original json file into a memory;
the first mapping module is used for mapping the original json file into a tree structure in the memory and determining that the json data node corresponds to a target node in the tree structure;
the first editing module is used for executing editing operation on the target node in the tree structure according to the operation parameters in the first editing instruction;
and the first storage module is used for storing the edited file with the tree structure into a file with a json format in the file system.
12. An electronic device comprising a processor and a memory, the memory having stored therein at least one instruction, at least one program, a set of codes, or a set of instructions, the at least one instruction, the at least one program, the set of codes, or the set of instructions being loaded and executed by the processor to implement the json data editing method of any one of claims 1-10.
13. A computer readable storage medium having stored therein at least one instruction, at least one program, a set of codes, or a set of instructions, which is loaded and executed by a processor to implement a json data editing method as claimed in any one of claims 1 to 10.
Priority Applications (1)
Application Number | Priority Date | Filing Date | Title |
---|---|---|---|
CN202210832208.4A CN115238655A (en) | 2022-07-14 | 2022-07-14 | Json data editing method and device |
Applications Claiming Priority (1)
Application Number | Priority Date | Filing Date | Title |
---|---|---|---|
CN202210832208.4A CN115238655A (en) | 2022-07-14 | 2022-07-14 | Json data editing method and device |
Publications (1)
Publication Number | Publication Date |
---|---|
CN115238655A true CN115238655A (en) | 2022-10-25 |
Family
ID=83673661
Family Applications (1)
Application Number | Title | Priority Date | Filing Date |
---|---|---|---|
CN202210832208.4A Pending CN115238655A (en) | 2022-07-14 | 2022-07-14 | Json data editing method and device |
Country Status (1)
Country | Link |
---|---|
CN (1) | CN115238655A (en) |
Cited By (2)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN116108235A (en) * | 2023-02-20 | 2023-05-12 | 上海安博通信息科技有限公司 | Path acquisition method and device for tree structure and processing equipment |
CN117908926A (en) * | 2024-01-10 | 2024-04-19 | 永联科技(常熟)有限公司 | Upgrade file generation method, device, equipment and storage medium |
-
2022
- 2022-07-14 CN CN202210832208.4A patent/CN115238655A/en active Pending
Cited By (3)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN116108235A (en) * | 2023-02-20 | 2023-05-12 | 上海安博通信息科技有限公司 | Path acquisition method and device for tree structure and processing equipment |
CN116108235B (en) * | 2023-02-20 | 2023-11-10 | 上海安博通信息科技有限公司 | Path acquisition method and device for tree structure and processing equipment |
CN117908926A (en) * | 2024-01-10 | 2024-04-19 | 永联科技(常熟)有限公司 | Upgrade file generation method, device, equipment and storage medium |
Similar Documents
Publication | Publication Date | Title |
---|---|---|
US7661053B2 (en) | Methods and apparatus for patternizing device responses | |
US20200104241A1 (en) | Behavior driven development integration with test tool | |
CN109614329B (en) | Software test case aided design method based on interface control file | |
CN115238655A (en) | Json data editing method and device | |
CN112307124B (en) | Database synchronous verification method, device, equipment and storage medium | |
CN113434395B (en) | Automatic generation method, device, equipment and medium for test cases | |
CN115658452A (en) | Buried point checking method, buried point checking device, readable storage medium and electronic equipment | |
CN114116745A (en) | Interface change identification and display method, device, equipment and medium | |
US11556531B2 (en) | Crux detection in search definitions | |
CN111444208B (en) | Data updating method and related equipment | |
US11176022B2 (en) | Health diagnostics and analytics for object repositories | |
CN112667502A (en) | Page testing method, device and medium | |
CN115795193A (en) | Page data extraction method and page automatic testing method | |
CN113037521B (en) | Method for identifying state of communication equipment, communication system and storage medium | |
CN114816364A (en) | Method, device and application for dynamically generating template file based on Swagger | |
CN115168124A (en) | Stability test method and device for concurrently executing multiple files on hard disk, computer equipment and medium | |
CN113656321A (en) | Automated testing method, system, device and medium | |
CN113778997B (en) | Database version management method, system and equipment | |
CN115599976B (en) | User grouping method, device, electronic equipment and storage medium | |
CN112463896A (en) | Archive cataloging data processing method and device, computing equipment and storage medium | |
CN118034777B (en) | FTTR-based log management and version control method, FTTR-based log management and version control device, FTTR-based log management and version control equipment and medium | |
CN115237936B (en) | Method, device, storage medium and equipment for detecting fields in SQL (structured query language) statement | |
CN113268412B (en) | Control analysis method, device, equipment and medium for Web system test case recording | |
CN113220800B (en) | ANTLR-based data field blood-edge analysis method and device | |
US11948024B2 (en) | Automated dynamic payload testing of OData APIs |
Legal Events
Date | Code | Title | Description |
---|---|---|---|
PB01 | Publication | ||
PB01 | Publication | ||
SE01 | Entry into force of request for substantive examination | ||
SE01 | Entry into force of request for substantive examination |