如何从Json Object获取价值......?

时间:2014-03-19 10:31:04

标签: java json

如果JsonArray类型为'个人'?如何从json对象获取值?我想要一个像这样的名字对:

String s = "February" ;
int i = 3;

我真的很感激如何进一步的建议。

提前致谢, 桑卡尔

String monthArray[] = { "January", "February", "March", "April", "May",
            "June", "July", "August", "September", "October", "November",
            "December" };

    System.out.println("Size of month array:-->> " + monthArray.length);

    JSONObject jbj = new JSONObject();
    JSONObject jbj1 = new JSONObject();
    JSONObject jbj2 = new JSONObject();

    JSONObject general = new JSONObject();

    JSONArray jarray = new JSONArray();
    JSONArray jarray1 = new JSONArray();
    JSONArray jarray2 = new JSONArray();

    JSONObject finalGen = new JSONObject();

    jbj.put("February", 3);
    jbj.put("March", 15);
    jbj.put("April", 6);
    jbj.put("July", 10);
    jbj.put("December", 5);

    jbj1.put("February", 3);
    jbj1.put("March", 14);
    jbj1.put("April", 23);
    jbj1.put("May", 6);

    jbj2.put("February", 33);
    jbj2.put("March", 12);
    jbj2.put("May", 4);
    jbj2.put("June", 7);

    jarray.add(jbj);
    jarray1.add(jbj1);
    jarray2.add(jbj2);

    general.put("Personal", jarray);
    general.put("Common", jarray1);
    general.put("Management", jarray2);
    finalGen.put("FinalResultSet", general);

    System.out.println("\n Personal Value :-->> " + finalGen.toString(3));

    JSONObject finalson = JSONObject.fromObject(finalGen
            .get("FinalResultSet"));
    JSONArray jarra = finalson.getJSONArray("Personal");

    for (int i = 0; i < jarra.size(); i++) {
        JSONObject jb = jarra.getJSONObject(i);

        for (int p = 0; p < monthArray.length; p++) {

            String s = monthArray[p];

            System.out.println("\n VALUE OF STR " + s);
            String t = jb.getString(s);

            System.out.println("\n  VALUE OF STR Json Str " + t);

        }
    }

    System.out.println("Personal String " + jarra);

}

我的输出: -

Size of month array:-->> 12

Personal Value :-->> {"FinalResultSet": {
    "Personal": [   {
      "February": 3,
      "March": 15,
      "April": 6,
      "July": 10,
      "December": 5
   }],
   "Common": [   {
      "February": 3,
      "March": 14,
      "April": 23,
      "May": 6
   }],
   "Management": [   {
      "February": 33,
      "March": 12,
      "May": 4,
      "June": 7
   }]
}}

VALUE OF STR January

例外: -

Exception in thread "main" net.sf.json.JSONException: JSONObject["January"] not found.
    at net.sf.json.JSONObject.getString(JSONObject.java:2177)
    at JFreechartDemo.XMLData.main(XMLData.java:68)

2 个答案:

答案 0 :(得分:1)

你正试图通过键&#34; 1月&#34;获得字段的价值。在你的for循环中,但没有这样的字段。 您应该首先将这样的键值对放入JSON对象中。

查看JSONObject get方法文档。那里有类似的东西:

Throws:
JSONException - if the key is not found.

因为如果没有键,get方法会抛出异常,所以在几个月内遍历整个数组并尝试猜测特定月份是否在JSON内部作为键是不是一个好主意。如果需要,可以执行此操作,但应首先使用has方法验证月份是否确实存在。

另一方面,您可以从JSONObject获取所有键的名称,然后仅对这些名称调用get方法。

答案 1 :(得分:0)

:113:14: error: assignment makes integer from pointer without a cast [-Werror]
  *woolArg[0] = "-p 3";
              ^