0

I am trying to write a simple Java program which writes JSON data to a file. I am compiling it with the JSON simple jar file but I am getting an error. Below is what I have done:

//JsonSimpleWriteExample.java

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

import java.io.FileWriter;
import java.io.IOException;

public class JsonSimpleWriteExample {

public static void main(String[] args) {

    JSONObject obj = new JSONObject();
    obj.put("name", "mkyong.com");
    obj.put("age", new Integer(100));

    JSONArray list = new JSONArray();
    list.add("msg 1");
    list.add("msg 2");
    list.add("msg 3");

    obj.put("messages", list);

    try (FileWriter file = new FileWriter("f:\\test.json")) {

        file.write(obj.toJSONString());
        file.flush();

    } catch (IOException e) {
        e.printStackTrace();
    }

    System.out.print(obj);

  }

 }

To compile the above program in a terminal, I am doing:

javac -cp json-simple-1.1.1.jar JsonSimpleWriteExample.java

I downloaded the json-simple-1.1.1.jar file from http://www.java2s.com/Code/Jar/j/json-simple.htm

I am getting the below error:

Note: JsonSimpleWriteExample.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

I am using MacOS. Can anybody help me figure out the issue.

When I try running my class file I get the below error:

$ java JsonSimpleWriteExample

Error that I got:

  Exception in thread "main" java.lang.NoClassDefFoundError: org/json/simple/JSONObject
at JsonSimpleWriteExample.main(JsonSimpleWriteExample.java:11)
  Caused by: java.lang.ClassNotFoundException: org.json.simple.JSONObject
  at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
  at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
  at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
  at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
  ... 1 more
user2567857
  • 483
  • 7
  • 25
  • That doesn't look like error, but warning. Errors prevents us from compiling, warnings inform us about potential problems. Raw types are one of them. You are informed that compiler can't assist you with type safety. But compiler should be able to generate `.class` files, so you should be able to run this code (and potentially face some *exceptions* if you messed up without compiler help which you ware *warned* about) – Pshemo Apr 02 '17 at 20:20
  • you need to include the jar in the class path using -cp json-simple.... – bichito Apr 03 '17 at 00:58
  • @efekctive I am ready doing that while creating the cass file: `javac -cp json-simple-1.1.1.jar JsonSimpleWriteExample.java` – user2567857 Apr 03 '17 at 02:42
  • 1
    compiling has nothing to do with running – bichito Apr 03 '17 at 06:53

2 Answers2

0

As you have it, you compile using the external jar file, so that the compiler finds the symbols you use during compilation time.

javac -cp json-simple-1.1.1.jar JsonSimpleWriteExample.java

The same has to be done at runtime:

java JsonSimpleWriteExample -cp ./json-simple-1.1.1.jar

In your example, this is the output:

{"name":"mkyong.com","messages":["msg 1","msg 2","msg 3"],"age":100}

As @Pshemo said, those "errors" you get are actually warnings. You can see more about unchecked or unsafe ops here and here.

Community
  • 1
  • 1
Miguel Jiménez
  • 1,276
  • 1
  • 16
  • 24
0
import org.json.simple.JSONObject;

JSONObject jsonObject = (JSONObject) yourOrginalObject;

//code for get all keys from json
Set<String> keys=jsonObject.keySet();

//iterate the keys

//get values by passing keys
jsonObject.get(pass individual keys);
BartoszKP
  • 34,786
  • 15
  • 102
  • 130