11

I need to be able to read a JSON string from a file and parse it.

I want to know if the string is a "well formed" JSON. If so, I need to be able to read all the name value pairs.

Is there a JSON library that comes bundled with Java itself?

I would prefer something that comes with the standard Java distribution instead of downloading another external library.

I am using JDK 1.6.

user18619318
  • 103
  • 10
davison
  • 335
  • 1
  • 3
  • 16
  • Not to my knowledge. Most of the JSON parsers for Java are listed at json.org, at the bottom of the page. – Hot Licks Sep 29 '14 at 17:25
  • Some people seem to use rhino http://stackoverflow.com/questions/10288669/access-rhinos-native-json-stringify-from-java/10837358 – Richard Chambers Sep 29 '14 at 17:29
  • has anybody used json-simple , its a json tool kit from google https://code.google.com/p/json-simple/ – davison Sep 29 '14 at 17:35
  • 1
    I have used json-simple. It seems verbose (compared to Python), but an effective and easy-to-use tool for JSON. – Chris Sep 29 '14 at 18:23
  • @davison Only because it may be hosted on Google Code, does not mean it's "from google". Google's JSON parser is Gson, btw. – qqilihq Sep 29 '14 at 18:36
  • 1
    @qqilihq thanks for pointing that out. updated my answer so that ppl are not misled. Thanks again!! – davison Sep 29 '14 at 18:39

2 Answers2

6

Try it approach without using external library

http://blog.julienviet.com/2011/12/26/json-to-java-with-jdk6/

EDITED

GitHub is more reliable source of the code from specified link. https://gist.github.com/vietj/1521692

The key is to use javascript to parse, it can be invoked with:

public class JSON2Java {

   private static final ScriptEngine jsonParser;

   static
   {
      try
      {
         String init = read(Tools.class.getResource("json2java.js"));
         ScriptEngine engine = new ScriptEngineManager().getEngineByName("JavaScript");
         engine.eval(init);
         jsonParser = engine;
      }
      catch (Exception e)
      {
         // Unexpected
         throw new AssertionError(e);
      }
   }

   public static Object parseJSON(String json)
   {
      try
      {
         String eval = "new java.util.concurrent.atomic.AtomicReference(toJava((" + json + ")))";
         AtomicReference ret = (AtomicReference)jsonParser.eval(eval);
         return ret.get();
      }
      catch (ScriptException e)
      {
         throw new RuntimeException("Invalid json", e);
      }
   }
}

Javascript part, json2java.js:

toJava = function(o) {
  return o == null ? null : o.toJava();
};
Object.prototype.toJava = function() {
  var m = new java.util.HashMap();
  for (var key in this)
    if (this.hasOwnProperty(key))
      m.put(key, toJava(this[key]));
  return m;
};
Array.prototype.toJava = function() {
  var l = this.length;
  var a = new java.lang.reflect.Array.newInstance(java.lang.Object, l);
  for (var i = 0;i < l;i++)
    a[i] = toJava(this[i]);
  return a;
};
String.prototype.toJava = function() {
  return new java.lang.String(this);
};
Boolean.prototype.toJava = function() {
  return java.lang.Boolean.valueOf(this);
};
Number.prototype.toJava = function() {
  return java.lang.Integer(this);
};
Basilevs
  • 22,440
  • 15
  • 57
  • 102
kemenov
  • 409
  • 4
  • 13
  • I wish I could upvote this!! Thanks!! – davison Sep 29 '14 at 17:27
  • 1
    This [answer appears to be in another castle](http://meta.stackexchange.com/questions/225370/your-answer-is-in-another-castle-when-is-an-answer-not-an-answer). Instead of just linking to an external resource, the relevant material should be posted here, giving proper attribution. This will allow the answer to remain relevant even if the link goes dead in the future. – Thomas Owens Sep 29 '14 at 17:27
  • @davison Then you can accept my request.:) – kemenov Sep 29 '14 at 17:30
  • @Thomas Owens i give the link to Git repository. – kemenov Sep 29 '14 at 17:36
  • 2
    Hmm, I pasted the code, but without packages it is useless. Could someone finish this and add proper imports? Also read() method seem to be pseudocode, which could be replaced with actual implementation. – Basilevs Sep 29 '14 at 18:42
-2

There is the javax.script Api which is built in to 1.6 http://docs.oracle.com/javase/6/docs/api/javax/script/package-summary.html

YoungerDryas
  • 114
  • 2
  • 10