-2

i have a JSON string and i want to write it as a JSON file(.json) inside of java. because my final application needs to import .json file not a string. (i am using spring to create a web app)

this is my json string:

String str = "{"polygons": {"landuse": ["__any__"],"building": ["__any__"],"shop": ["__any__"]}}";

and i want a file (str.json) inside of my java webapp not saving to pc and then importing.

do you have solution for this task?

thanks

jack
  • 521
  • 2
  • 6
  • 9
  • This will not compile. Please post a [Minimal, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve). – Eli Sadoff Nov 04 '16 at 23:25
  • 1
    Could you please rephrase the question? – Mick Mnemonic Nov 04 '16 at 23:25
  • could you please give me an script. – jack Nov 04 '16 at 23:34
  • No, we can/will not *give* you a script. This is not a free (or paid) code-writing service. Please read "[What topics can I ask about here?](http://stackoverflow.com/help/on-topic)". Besides, I'm not even sure what you want to script to do. If you have a JSON string and you want to write it to a JSON file, just write the string to a file. – Andreas Nov 05 '16 at 00:02

1 Answers1

0

well for this you can use GSON, and the classes you will use are JsonWriter and FileWriter.

An Example:

json: {"resource":[{"name":"Node1"}],"literals":[{"literal":["A","B","C","D"]}]}

then do it like this:

JsonWriter jsonWriter = null;
try {
    jsonWriter = new JsonWriter(new FileWriter("test.json"));
    jsonWriter.beginObject();
    jsonWriter.name("resource");
    jsonWriter.beginArray();
    jsonWriter.beginObject();
    jsonWriter.name("name");
    jsonWriter.value("Node1");
    jsonWriter.endObject();
    jsonWriter.endArray();
    jsonWriter.name("literals");
    jsonWriter.beginArray();
    jsonWriter.beginObject();
    jsonWriter.name("literal");
    jsonWriter.beginArray();
    jsonWriter.value("A");
    jsonWriter.value("B");
    jsonWriter.value("C");
    jsonWriter.value("D");
    jsonWriter.endArray();
    jsonWriter.endObject();
    jsonWriter.endArray();
    jsonWriter.endObject();

Hope help you, bye bye.

ccamol
  • 119
  • 1
  • 7