从字符串java获取值

时间:2016-08-18 08:53:30

标签: java arrays

我有这条线

String tabledata = request.getParameter("tabledata");

当我做的时候

out.println(tabledata);

我得到了

[{
    "year": "2015",
    "org": "1",
    "sub_org": "2",
    "sub_org_amt": "1"
}, {
    "year": "2015",
    "org": "1",
    "sub_org": "1",
    "sub_org_amt": "1"
}]

如何获得年度价值?

我试过

out.println(tabledata[0].year);

我得到array required, but String found

UPDATE

尝试

JSONArray newdata = new JSONArray(tabledata);


out.println(newdata.length());
out.println(newdata.getString(1));
for (int i = 0; i < newdata.length(); i++) {
    out.println(newdata.getString(i));


}

但我得到了

{
    "year": "2015",
    "org": "1",
    "sub_org": "2",
    "sub_org_amt": "1"
}

{
    "year": "2015",
    "org": "1",
    "sub_org": "1",
    "sub_org_amt": "1"
}

我想要一年的价值,我希望自己成为2015

我用过这个。它会起作用

JSONArray newdata = new JSONArray(tabledata);


out.println(newdata.length());
out.println(newdata.getString(1));
for (int i = 0; i < newdata.length(); i++) {
    out.println(newdata.getJSONObject(i).get("year"));


}

0 个答案:

没有答案