In the rest Interface testing process, a given template is required to generate a batch of JSON strings with data
Examples of Json templates such as the following
{"School": {"num": "122121", "addr": "@addr"}, "name": "@name", "Age": "@age"}
Examples of source data such as the following
Test |
Name |
Age |
Addr |
Test1 |
Name1 |
12 |
First Avenue |
Test2 |
Name2 |
13 |
First Avenue |
Its output should be as follows
TestCase |
Json |
Test1 |
{"School": {"num": "122121", "addr": " First Avenue "}, "name": "Name1", "Age": "" "} |
Test2 |
{"School": {"num": "122121", "addr": " First Avenue "}, "name": "Name2", "Age": "$"} |
If you use the replace function of String , replace a tag to Create a new string, if it is to generate a large amount of data, Must affect performance. I also thought about using a String format function like the one where you want to replace the same %s, and then passing an array to him in sequence, but this has to tick the order of the data, Writing data is not very convenient. Want to function as an example, tagged with tags, easy to write data when positioning.
I used the method is to find the location of all the substring of the tag, and then in the whole string of these substrings are cut off the generation of sub-strings, and then generated, you can use these substrings to join the new string, the specific code is as follows,
String Json=this. Readjson ();//map4int map4str for sorting treemap<integer,integer> map4int = new treemap<integer,integer> (); treemap<integer,string> map4str = new treemap<integer,string> (); List<string> sortstring=new arraylist<string> (); The title is used to hold the string to be replaced//table holds the replacement string and its corresponding data to be replaced liststring[] title=er.readexceltitle (); Map<string,list<string>>table=er.readexcelcontent ();//Find the position of the string to replace in the JSON string and sort for (string t:title) { int Index=json.indexof ("@" +t), if (index!=-1) {map4int.put (index, Index+t.length ()); Map4str.put (index, t);}} Sortstring is used to save all strings to be replaced for (entry<integer,string> E:map4str.entryset ()) {Sortstring.add (E.getvalue ()) at the occurrence location;} STRs is used to save the JSON to remove all substrings after the replacement string list<string>strs=new arraylist<string> (); int Start=0;int End=0;for ( Map.entry<integer,integer> E:map4int.entryset ()) {End=e.getkey (); if (End>start) {Strs.add (json.substring ( Start, end)); Start=e.getvalue () +1;}} Strs.add (json.substring (start));//The substring is then stitched together into an entire string for each set of data StringBuilder Sb=new StringBuilder (); List<string> jsons=new arraylist<string> (); for (int. i=0;i<rowcnt;i++) {for (int j=0;j<strs.size ()-1 ; j + +) {Sb.append (Strs.get (j)); Sb.append (Table.get (Sortstring.get (j)). Get (i)); Sb.append (Strs.get (Strs.size ()-1)) Jsons.add (sb.tostring ()); Sb.delete (0, Sb.length ());}
Java stitching substrings in a way that makes string substitution to generate JSON