Java将JSON读取为关联数组,如javascript和PHP(['key'] ['subKey'])

时间:2018-05-05 12:43:50

标签: java json associative-array

我正在编写一个java程序,将 Json 作为关联数组读取,就像我们在 javascript PHP 中一样。我完成了90%但是当我尝试从 Json对象读取数组时出现问题。

这是data.json文件,其中包含学生记录

{
"students": {
    "2334": {
        "name": {
            "firstname": "umer",
            "lastname": "farooq"
        },
        "subject": {
            "marks": {
                "maths": {
                    "total": 100.0,
                    "obtained": 70.0
                },
                "computer": {
                    "total": 100.0,
                    "obtained": 96.0
                }
            }
        },
        "lang": ["java", "javascript", "C#"]
    },
    "1003": {
        "name": {
            "firstname": "yasir",
            "lastname": "khan"
        },
        "subject": {
            "marks": {
                "maths": {
                    "total": 100.0,
                    "obtained": 80.0
                },
                "computer": {
                    "total": 100.0,
                    "obtained": 60.0
                }
            }
        },
        "lang": ["C++", "PHP", "C#"]
    },
    "1233": {
        "name": {
            "firstname": "Mubarak",
            "lastname": "Amin"
        },
        "subject": {
            "marks": {
                "maths": {
                    "total": 100.0,
                    "obtained": 70.0
                },
                "computer": {
                    "total": 100.0,
                    "obtained": 50.0
                }
            }
        },
        "lang": ["Ruby", "javascript", "C"]
    }
}

}

要获取学生在数学中使用ID 1233获得的分数,我会简单地将字符串['students']['1233']['subject']['marks']['maths']['obtained']传递给getValue()方法

     JSONAssociativeArrayReader parser = new JSONAssociativeArrayReader();
     parser.setJSONFile(new File("data.json"));
     double marksObtainedInMaths = (double) parser.getValue("['students']['1233']['subject']['marks']['maths']['obtained']");

     System.out.println("Marks obtained (maths) = " + marksObtainedInMaths);

上面的代码生成Marks obtained (maths) = 70.0这是正确的。当我尝试通过array方法从Json文件中获取getArray()时出现问题。

System.out.println("languages = " + parser.getArray("['students']['1233']['lang']") );
expected output : languages = [Ruby, javascript, C]

执行上述语句时发生以下错误

    Exception in thread "main" javax.script.ScriptException: TypeError: Cannot read property "lang" from undefined in <eval> at line number 1
    at jdk.nashorn.api.scripting.NashornScriptEngine.throwAsScriptException(NashornScriptEngine.java:467)
    at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:451)
    at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:403)
    at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:399)
    at jdk.nashorn.api.scripting.NashornScriptEngine.eval(NashornScriptEngine.java:155)
    at javax.script.AbstractScriptEngine.eval(Unknown Source)
    at JSONAssociativeArrayReader.getArray(Main.java:77)
    at Main.main(Main.java:30)
Caused by: <eval>:1 TypeError: Cannot read property "lang" from undefined
    at jdk.nashorn.internal.runtime.ECMAErrors.error(ECMAErrors.java:57)
    at jdk.nashorn.internal.runtime.ECMAErrors.typeError(ECMAErrors.java:213)
    at jdk.nashorn.internal.runtime.ECMAErrors.typeError(ECMAErrors.java:185)
    at jdk.nashorn.internal.runtime.ECMAErrors.typeError(ECMAErrors.java:172)
    at jdk.nashorn.internal.runtime.Undefined.get(Undefined.java:157)
    at jdk.nashorn.internal.scripts.Script$2$\^eval\_.:program(<eval>:1)
    at jdk.nashorn.internal.runtime.ScriptFunctionData.invoke(ScriptFunctionData.java:637)
    at jdk.nashorn.internal.runtime.ScriptFunction.invoke(ScriptFunction.java:494)
    at jdk.nashorn.internal.runtime.ScriptRuntime.apply(ScriptRuntime.java:393)
    at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:446)
    ... 6 more

以下是JSONAssociativeArrayReader

的代码
class JSONAssociativeArrayReader 
{
     private ScriptEngineManager factory;
     private ScriptEngine engine;
     private static final String JSON_OBJECT_NAME = "jsonObject";
     private static final String JSON_ARRAY_NAME = "jsonArray";


    public JSONAssociativeArrayReader() throws ScriptException
    {
        factory = new ScriptEngineManager();
        engine = factory.getEngineByName("JavaScript");
    }

    public void setJSONFile(File jsonFile) throws IOException, ScriptException
    {

        String jsonStringFromFile = "";
        if(jsonFile != null) 
        {
            BufferedReader br = new BufferedReader(new FileReader(jsonFile));
            String line = "";
            while((line = br.readLine()) != null)
            {
                jsonStringFromFile += line;
            }

        }

        engine.eval("var " + JSON_OBJECT_NAME + " = " + jsonStringFromFile);

    }

    public ArrayList<Object> getArray(String associativeArray) throws ScriptException
    {
        ArrayList<Object> objects = new ArrayList<>();

        engine.eval("var " + JSON_ARRAY_NAME + " = " + associativeArray);

        engine.eval("var arraylength = " +  JSON_ARRAY_NAME + ".length");
        int arrayLength = (int) engine.eval("arraylength");

        for (int i = 0; i < arrayLength; i++)
        {
            engine.eval(" var arrayElement = " + JSON_ARRAY_NAME + "["+i+"]");
            objects.add(engine.get("arrayElement"));
        }

        return objects;

    }



    public Object getValue(String associativeArray) throws ScriptException
    {
        return engine.eval(JSON_OBJECT_NAME+associativeArray);
    }


}

我做错了什么?

1 个答案:

答案 0 :(得分:0)

据我所知,你的问题在

JSON_ARRAY_NAME + " = " + associativeArray);

您忘记了JSON_OBJECT_NAME,只是为"['students']['1233']['lang']"

的值分配了一个var

换句话说,它需要得到

的结果
jsonObject['students']['1233']['lang']

但是,在Java中的几乎所有JSON解析库中,您都会执行类似

的操作
 jsonObject.getObject("students")
    .getObject("1233")
    .getArray("lang")

或者使用Gson / Jackson,你可以轻松编写一个模型来实现

jsonObject.getStudent("1233").getLanguages()