I have a locally stored json file as c:\path\jsontext.json. I have a jsp page and .js file, In my .js file I have a javascript object, the data of this javascript object is in json format and i would like to append it to the jsontext.json file
But can figure out a way to do this.
Here is my jsontext.json
[
{
"Identifier": "1",
"Label": "pratik",
"Categories": "Standard",
"UpdatedBy": "lno",
"UpdatedAt": "01-02-2013"
},
{
"Identifier": "2",
"Label": "2013",
"Categories": "Standard",
"UpdatedBy": "lno",
"UpdatedAt": "01-02-2013"
}
]
supposing in my .js file i have a javascript object
var JSObject=("[{" +'"example1"'+"},"+ "{" + '"example2"' + "}]");
I want to append this data in my jsontext.json file,
how should I go about it.
I looked at some resources and found that I would require a server side programming language to get this done.
If I try doing this in a java code, then the code is easy to write. The only problem then is the java method will be execute first and then the .js file.
If there is a way - a clickevent on the .js file which can call the java method then it would also work for me.
I have this java code.
import java.io.FileWriter;
import java.io.IOException;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
public class JsonSimpleExample {
public static void main(String[] args) {
JSONObject JSONObject = new JSONObject();
try {
FileWriter file = new FileWriter("c:\\jsontext.json");
file.write(JSONObject.toJSONString());
file.flush();
file.close();
} catch (IOException e) {
e.printStackTrace();
}
System.out.print(JSONObject);
}
}
Where should I include this java code in order to write the item into jsontext.json file.? and. IS there are method in Jquery for this??
"[{" +'"example1"'+"},"+ "{" + '"example2"' + "}]"
wont give you json. – Musa Jun 14 '13 at 4:20