Convert a list collection to a JSON file and export:
Data collection:
New Arraylist<object>(); MapNew hashmap<>(); Agencymap.put ("Agencyname", agencyname); Agencymap.put ("agencyaddress", agencyaddress); Agencymap.put ("CompanyName", companyName); Agencymap.put ("Logoimageid", Logoimageid); Agencymap.put ("auctionaddress", agencyauctionaddress); Agencymap.put ("Logoimage", logoimage); Agencylist.add (AGENCYMAP);
Convert the collection data to a JSON string (of course the map collection is also possible):
Jsonarray jsonobject = jsonarray.fromobject (agencylist); = jsonobject.tostring (); " /filestorage/download/json "," agency ");
Public classCreatefileutil {/*** generate. json format Files*/ Public Static Booleancreatejsonfile (String jsonstring, String FilePath, String fileName) {//Mark whether the file was generated successfully BooleanFlag =true; //stitching file Full pathString FullPath = filePath + file.separator + fileName + ". JSON"; //generate JSON-formatted files Try { //guaranteed to create a new fileFile File =NewFile (FullPath); if(!file.getparentfile (). exists ()) {//If the parent directory does not exist, create a parent directoryfile.getparentfile (). Mkdirs (); } if(File.exists ()) {//if it already exists, delete the old fileFile.delete (); } file.createnewfile (); if(Jsonstring.indexof ("'")!=-1){ //Escape the single quotation mark because the string type in the JSON string can be quoted in single quotation marksjsonstring = Jsonstring.replaceall ("'", "\ \" ")); } if(Jsonstring.indexof ("\" ")!=-1){ //escape the double quotes because the string types in the JSON string can be quoted in single quotesjsonstring = Jsonstring.replaceall ("\" "," \\\ ""); } if(Jsonstring.indexof ("\ r \ n")!=-1){ //convert carriage return to newline because strings in the JSON string cannot have an explicit return line breakjsonstring = Jsonstring.replaceall ("\ r \ n", "\\u000d\\u000a"); } if(Jsonstring.indexof ("\ n")!=-1){ //convert a line break because a string in the JSON string cannot have an explicit line breakjsonstring = Jsonstring.replaceall ("\ n", "\\u000a"); } //formatting a JSON stringJsonstring =Jsonformattool.formatjson (jsonstring); //writes a formatted string to a fileWriter Write =NewOutputStreamWriter (NewFileOutputStream (file), "UTF-8"); Write.write (jsonstring); Write.flush (); Write.close (); } Catch(Exception e) {flag=false; E.printstacktrace (); } //returns whether the token was successful returnFlag; } }
Public classJsonformattool {/*** Units indent strings. */ Private StaticString SPACE = ""; /*** Returns the formatted JSON string. * * @paramJSON unformatted JSON string. * @returna formatted JSON string. */ Public Staticstring Formatjson (String json) {StringBuffer result=NewStringBuffer (); intLength =json.length (); intNumber = 0; CharKey = 0; //iterates through the input string. for(inti = 0; i < length; i++) { //1. Gets the current character. Key =Json.charat (i); //2, if the current character is the front bracket, the preceding curly braces to do the following processing: if(Key = = ' [') | | (Key = = ' {'))) { //(1) If there are characters in front, and the character is ":", Print: NewLine and indent character strings. if((i-1 > 0) && (json.charat (i-1) = = ': ') {result.append (' \ n '); Result.append (indent (number)); } //(2) Print: The current character. result.append (key); //(3) The front bracket, the front curly brace, must be followed by a newline. Print: Line break. Result.append (' \ n '); //(4) Each occurrence of a front bracket, the front curly brace, the number of indents increased once. Print: New line indent. number++; Result.append (indent (number)); //(5) Proceed to the next cycle. Continue; } //3, if the current character is the rear bracket, after the curly braces do the following processing: if(key = = '] ') | | (Key = = '} ')) { //(1) The rear brackets, after curly braces, must be wrapped in front. Print: Line break. Result.append (' \ n '); //(2) Each appearance of the rear bracket, after curly braces, the number of indents decreased once. Print: Indent. number--; Result.append (indent (number)); //(3) Print: The current character. result.append (key); //(4) If there are characters after the current character, and the characters are not ",", Print: Line wrapping. if((((i + 1) < length) && (Json.charat (i + 1)! = ', ') {result.append (' \ n '); } //(5) continue the next cycle. Continue; } //4, if the current character is a comma. The comma is followed by a newline and indented without changing the indent count. /*if ((key = = ', ')) {result.append (key); Result.append (' \ n '); Result.append (indent (number)); Continue }*/ //5, Print: The current character. result.append (key); } returnresult.tostring (); } /*** Returns the indentation string for a specified number of times. Each time you indent three spaces, space. * * @paramnumber indent count. * @returnA String that specifies the number of indents. */ Private StaticString Indent (intNumber ) {StringBuffer result=NewStringBuffer (); for(inti = 0; I < number; i++) {result.append (SPACE); } returnresult.tostring (); }}
Of course, it involves escaping character handling issues.
Java generates and exports JSON files