与 Java 捆绑的标准 JSON 解析器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26105426/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Standard JSON parser that comes bundled with Java
提问by davison
I need to be able to read a JSON string from a file and parse it.
我需要能够从文件中读取 JSON 字符串并解析它。
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.
我想知道该字符串是否是“格式良好”的 JSON。如果是这样,我需要能够读取所有名称值对。
Is there a JSON library that comes bundled with Java itself?
是否有与 Java 本身捆绑在一起的 JSON 库?
I would prefer something that comes with the standard Java distribution instead of downloading another external library.
我更喜欢标准 Java 发行版附带的东西,而不是下载另一个外部库。
I am using JDK 1.6.
我正在使用 JDK 1.6。
回答by kemenov
Try it approach without using external library
在不使用外部库的情况下尝试它的方法
http://blog.julienviet.com/2011/12/26/json-to-java-with-jdk6/
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
GitHub 是来自指定链接的更可靠的代码来源。 https://gist.github.com/vietj/1521692
The key is to use javascript to parse, it can be invoked with:
关键是使用javascript来解析,可以通过以下方式调用:
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:
Javascript 部分,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);
};
回答by MaoKnight
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
有内置于 1.6 http://docs.oracle.com/javase/6/docs/api/javax/script/package-summary.html的 javax.script Api