Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/389.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JsonSimple:如何在Json提要中获取第二个对象_Java_Json - Fatal编程技术网

Java JsonSimple:如何在Json提要中获取第二个对象

Java JsonSimple:如何在Json提要中获取第二个对象,java,json,Java,Json,嗨,我试图使用Json Simple来选择“cheese”条目,但是当我尝试选择它时,我似乎返回了“null”。如果你能就如何做到这一点提供一些建议,那就太好了 以下是Json示例: String s="[\"coins\",{\"wallet\":{\"shoppinglist\":{\"cheese\":{\"ingrediants\":[\"milk\",{\"preservative1\":\"wax\"}]}}}}]"; 代码如下: System.out.println(

嗨,我试图使用Json Simple来选择“cheese”条目,但是当我尝试选择它时,我似乎返回了“null”。如果你能就如何做到这一点提供一些建议,那就太好了

以下是Json示例:

String s="[\"coins\",{\"wallet\":{\"shoppinglist\":{\"cheese\":{\"ingrediants\":[\"milk\",{\"preservative1\":\"wax\"}]}}}}]";
代码如下:

      System.out.println(s);
        System.out.println("=======decode=======");

      Object obj=JSONValue.parse(s);
      JSONArray array=(JSONArray)obj;
      System.out.println("======the 2nd element of array======");
      System.out.println(array.get(1));
      System.out.println();
      System.out.println("======the 1st element of array======");
      System.out.println(array.get(0));
      System.out.println();



      JSONObject obj2=(JSONObject)array.get(1);
      System.out.println("======field \"1\"==========");
      System.out.println(obj2.get("wallet"));  


      JSONObject obj3=(JSONObject) obj2.get("shoppinglist");
      System.out.println("======field \"2\"==========");
      System.out.println(obj3);  //This figure is returning null when I would like it to return the json object shopping list
它目前的产出是:

["coins",{"wallet":{"shoppinglist":{"cheese":{"ingrediants":["milk",{"preservative1":"wax"}]}}}}] =======decode======= ======the 2nd element of array====== {"wallet":{"shoppinglist":{"cheese":{"ingrediants":["milk",{"preservative1":"wax"}]}}}} ======the 1st element of array====== coins ======field "1"========== {"shoppinglist":{"cheese":{"ingrediants":["milk",{"preservative1":"wax"}]}}} ======field "2"========== null [“硬币”,“钱包”:{“购物清单”:{“奶酪”:{“原料”:[“牛奶”,“防腐剂”:“蜡”}}] =======解码======= =======阵列的第二个元素====== {“钱包”:{“购物清单”:{“奶酪”:{“原料”:[“牛奶”,“防腐剂”:“蜡”}}}}} =======阵列的第一个元素====== 硬币 =======字段“1”========== {“购物清单”:{“奶酪”:{“原料”:[“牛奶”,“防腐剂1”:“蜡”}}}} =======字段“2”========== 无效的
你跳过了筑巢的一步
obj2
具有“wallet”属性。“购物清单”更深一层

要获得预期结果,请使用以下方法:

JSONObject wallet       = (JSONObject) obj2.get("wallet");
JSONObject shoppinglist = (JSONObject) wallet.get("shoppinglist");
System.out.println(shoppinglist);

这将给我一个教训:试着做一个聪明人,在我的system.outs中工作,不要用好名字命名变量。吸取的教训!-谢谢!