JSON file editing

Source: Internet
Author: User

Import Java.io.File;

Import java.io.IOException;

Import Java.util.HashMap;

Import Java.util.Map;


Import Org.apache.commons.io.FileUtils;

Import Org.apache.commons.lang.StringUtils;


Import Com.alibaba.fastjson.JSON;

Import Com.alibaba.fastjson.JSONArray;

Import Com.alibaba.fastjson.JSONObject;

Import Com.google.gson.Gson;

Import Com.google.gson.GsonBuilder;

Import com.google.gson.JsonElement;

Import Com.google.gson.JsonParser;



public class Jsondatautils {

public static void edit (String f_json,string field,string fvalue,map<string,object> editdata)

{

String jsonlist = "";

try {

Jsonlist = org.apache.commons.io.FileUtils.readFileToString (new File (F_json));

Jsonobject data = Json.parseobject (jsonlist);

Jsonarray ja = Data.getjsonarray ("Rows");

int len = Ja.size ();

for (int i = 0;i<len;i++)

{

Jsonobject Jo = Ja.getjsonobject (i);

if (jo.getstring (field). Equals (Fvalue))

{

For (String Key:editData.keySet ())

{

Jo.put (Key, Editdata.get (key));

}

Ja.set (i, Jo);

}

}

Data.put ("Rows", JA);

Fileutils.writestringtofile (New File (F_json), Jsonformatter (Data.tojsonstring ()));

} catch (IOException e) {

E.printstacktrace ();

}

}

public static void Add (String f_json,map<string,object> addData)

{

String jsonlist = "";

try {

Jsonlist = org.apache.commons.io.FileUtils.readFileToString (new File (F_json));

Jsonobject data = Json.parseobject (jsonlist);

Jsonarray ja = Data.getjsonarray ("Rows");

Ja.add (AddData);

Data.put ("Rows", JA);

Data.put ("Total", Data.getinteger ("Total") +1);

Fileutils.writestringtofile (New File (F_json), Jsonformatter (Data.tojsonstring ()));

} catch (IOException e) {

E.printstacktrace ();

}

}


public static void Del (String f_json,string field,string fvalue)

{

String jsonlist = "";

try {

Jsonlist = org.apache.commons.io.FileUtils.readFileToString (new File (F_json));

Jsonobject data = Json.parseobject (jsonlist);

Jsonarray ja = Data.getjsonarray ("Rows");

int len = Ja.size ();

for (int i = 0;i<len;i++)

{

Jsonobject Jo = Ja.getjsonobject (i);

if (jo.getstring (field). Equals (Fvalue))

{

Ja.remove (Jo);

}

}

System.out.println (Ja.tojsonstring ());

Data.put ("Rows", JA);

Data.put ("Total", Data.getinteger ("Total")-1);

System.out.println (Data.tojsonstring ());

Fileutils.writestringtofile (New File (F_json), Jsonformatter (Data.tojsonstring ()));

} catch (IOException e) {

E.printstacktrace ();

}

}

public static void Batdel (String f_json,string field,string fvalues)

{

String jsonlist = "";

try {

Jsonlist = org.apache.commons.io.FileUtils.readFileToString (new File (F_json));

Jsonobject data = Json.parseobject (jsonlist);

Jsonarray ja = Data.getjsonarray ("Rows");

int len = Ja.size ();

string[] FV = Fvalues.split (",");

System.out.println (fvalues);

for (int i = 0;i<len;i++)

{

Jsonobject Jo = Ja.getjsonobject (i);

for (String FVALUE:FV)

{

if (Stringutils.isblank (Fvalue)

|| Stringutils.isempty (Fvalue))

{

Continue

}

if (jo.getstring (field). Equals (Fvalue))

{

Ja.remove (Jo);

Len-= 1;

System.out.println ("Delete a piece");

}

}

}

System.out.println (Ja.tojsonstring ());

Data.put ("Rows", JA);

Data.put ("Total", Data.getinteger ("Total")-fv.length);

System.out.println (Data.tojsonstring ());

Fileutils.writestringtofile (New File (F_json), Jsonformatter (Data.tojsonstring ()));

} catch (IOException e) {

E.printstacktrace ();

}

}

/**

*

* @comments formatted to Visual mode

* @time 2015-11-4 11:25:19

* @param uglyjsonstring

* @return

* @returnType String

* @modification

*/

public static string Jsonformatter (String uglyjsonstring) {

Gson Gson = new Gsonbuilder (). setprettyprinting (). Create ();

Jsonparser JP = new Jsonparser ();

Jsonelement JE = jp.parse (uglyjsonstring);

String prettyjsonstring = Gson.tojson (JE);

return prettyjsonstring;

}

public static void Main (string[] args) {

String F_json = "D:\\workspaces\\demo\\webroot\\data\\bgs\\txl.json";

map<string,object> editdata = new hashmap<string, object> ();

Editdata.put ("Txlstatu", "Consent to modify");

Jsondatautils.edit (F_json, "id", "txl-00001", editdata);

Editdata.put ("id", "txl-00011");

Jsondatautils.add (F_json, editdata);

Editdata.put ("id", "txl-00012");

Jsondatautils.add (F_json, editdata);

Jsondatautils.del (F_json, "id", "txl-00012");

Jsondatautils.batdel (F_json, "id", "txl-00011,txl-00012");

}


}


JSON file editing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.