【问题标题】:JSONArray being lost on encodeJSONArray 在编码时丢失
【发布时间】:2015-04-18 03:52:24
【问题描述】:

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

当某件事发生时,我正在尝试将 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"
        } 
     ]  
   }
 ]
}

当我说应该时,我的意思是预期的。添加的是“kicks”下的 JSONArray“2”。

但结果是这样的:

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

最终丢失的是“kicks”下的“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();
    }
}

【问题讨论】:

  • 从示例中我可以看出,不是附加到 json 数组,而是将整个内容添加为新的 json 对象。只需发布一些代码,以便我可以提供更多帮助。
  • 骗我,马上加。
  • 在“数据”中已经“踢”了,或者您正在此 sn-p 中新创建它。
  • 哦,糟糕,我不久前从 JSON 文件中删除了“数据”。它主要是一个示例,看看它是否有效,但它在所有运行中都存在,但这一定是因为它仍在代码中并被添加。

标签: java arrays json


【解决方案1】:

我们没有足够的信息,但听起来您只想追加数据到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
                                )
                        )
                )
        )
)

【讨论】:

  • 不是java,但我可能会尝试理解它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-03
  • 1970-01-01
  • 1970-01-01
  • 2012-12-15
相关资源
最近更新 更多