Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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??

share|improve this question
    
"[{" +'"example1"'+"},"+ "{" + '"example2"' + "}]" wont give you json. –  Musa Jun 14 '13 at 4:20
    
thats written just to explain my question. I know thats not the actual json. –  patz Jun 14 '13 at 4:26

1 Answer 1

If you need to get access to local file system from browser you might want to check File API

You cannot simply access clients computer from jQuery/JavaScript. User will need to permission that as well.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.