JSONArray在编码时丢失了

时间:2015-04-18 03:52:24

标签: java arrays json

好的,这可能会令人困惑。

当某件事发生时,我试图将jsonarray添加到jsonobject。目前,JSON文件看起来有点像这样

{
  "sweg":true,
  "bans":[
    {
      "1":[
         {
          "reason":"Hacking"
         }
      ]
    }
  ],
  "kicks":[
    {
      "1":[
         {
          "reason":"testing"
         }
      ]
    }
  ]
}

另外,对不起,如果文件格式不好。我不得不手动隔开它。

接下来,它应该看起来有点像

{
  "sweg":true,
  "bans":[
    {
      "1":[
         {
          "reason":"Hacking"
         }
      ]
    }
  ],
  "kicks":[
    {
     "1":[
        {
         "reason":"testing"
        }
     ],
     "2":[
        {
         "reason":"meow"
        } 
     ]  
   }
 ]
}

当我说应该,我的意思是什么。正在添加的是JSONArray" 2"在"踢"。

但事实证明是这样的:

{
  "sweg":true,
  "bans":[
     {
      "1":[
         {
          "reason":"Hacking"
         }
      ] 
     }
  ],
 "kicks":[
   {
     "2":[
        {
         "reason":"meow"
        }
     ]
   }
 ]
}

最终最终失去的是" 1" "踢"

下的阵列 编辑:忘了代码,这里是:

    public static void main(String[] args) {
    JSONParser jsonParser = new JSONParser();
    JSONObject jsonObject = new JSONObject();
    try {
        Object obj = jsonParser.parse(new FileReader("testing.json"));
        jsonObject = (JSONObject) obj;
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    JSONObject data = new JSONObject();
    jsonObject.put("data", data);
    JSONArray jsonArray11 = new JSONArray();
    jsonObject.put("kicks", jsonArray11);
    JSONObject placeHolder2 = new JSONObject();
    jsonArray11.add(placeHolder2);
    JSONArray kick2 = new JSONArray();
    placeHolder2.p(2, kick2);
    JSONObject jsonObject1 = new JSONObject();
    kick2.add(jsonObject1);
    jsonObject1.put("reason", "meow");
    try {
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(new File("testing.json"), false));
        bufferedWriter.write(jsonObject.toJSONString());
        bufferedWriter.flush();
        bufferedWriter.close();
    } catch (IOException ex) {
        ex.printStackTrace();
    }
}

1 个答案:

答案 0 :(得分:0)

我们没有足够的信息,但听起来您只想追加数据到kicks。你可以这样做:

$json = '{
  "sweg":true,
  "bans":[
    {
      "1":[
         {
          "reason":"Hacking"
         }
      ]
    }
  ],
  "kicks":[
    {
     "1":[
        {
         "reason":"testing"
        }
     ]
   }
 ]
}';
$jsonObj = json_decode($json);

$meow = '[
        {
         "reason":"meow"
        } 
     ]';  
$meowObj = json_decode($meow);

/* This will count the fields in kicks[0], increment, and create a few field 
   and name the new field the resulting number, then insert the new object. */
$jsonObj->kicks[0]->{count((array)$jsonObj->kicks[0])+1} = $meowObj;

print_r($jsonObj);

这将导致:

stdClass Object
(
    [sweg] => 1
    [bans] => Array
        (
            [0] => stdClass Object
                (
                    [1] => Array
                        (
                            [0] => stdClass Object
                                (
                                    [reason] => Hacking
                                )

                        )

                )

        )

    [kicks] => Array
        (
            [0] => stdClass Object
                (
                    [1] => Array
                        (
                            [0] => stdClass Object
                                (
                                    [reason] => testing
                                )

                        )

                    [2] => Array
                        (
                            [0] => stdClass Object
                                (
                                    [reason] => meow
                                )
                        )
                )
        )
)