0

我有这种类型的 JSON:

{
    "stampi": 
    [
        {
            "nome": "Ovale Piccolo 18.2x13.5cm",
            "lunghezza": 18.2,
            "larghezza": 13.5,
            "altezza": 4,
            "volume": 786.83
        },                      
         {
            "nome": "Ovale Grande 22.5x17.4cm",
            "lunghezza": 22.5,
            "larghezza": 17.4,
            "altezza": 4,
            "volume": 1246.54
        }                                                 
    ]


}

通常我会阅读以下代码:

        StringBuffer sb = new StringBuffer();
        BufferedReader br = null;
        try {
            br = new BufferedReader(new InputStreamReader(getAssets().open("stampi.json")));

            String temp;
            while ((temp = br.readLine()) != null)
                sb.append(temp);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                br.close(); // stop reading
            } catch (IOException e) {
                e.printStackTrace();
            }
        }           
        myjson_stampi = sb.toString();  

并在程序内部使用数组之后。我创建了一个在 JSON 文件中添加新值的菜单,但我遇到了问题……这是代码:

              StringBuffer sb = new StringBuffer();
              BufferedReader br = null;
              try {
                  br = new BufferedReader(new InputStreamReader(getAssets().open("stampi.json")));

                  String temp;
                  while ((temp = br.readLine()) != null)
                      sb.append(temp);
              } catch (IOException e) {
                  e.printStackTrace();
              } finally {
                  try {
                      br.close(); // stop reading
                  } catch (IOException e) {
                      e.printStackTrace();
                  }
              }           
              myjson_stampi = sb.toString();      

              try {                       
                  // Creating JSONObject from String
                  JSONObject jsonObjMain = new JSONObject(myjson_stampi);

                  // Creating JSONArray from JSONObject
                  JSONArray objNames = jsonObjMain.names();
                  System.out.println(objNames.toString());

                  jsonArray_stampi = jsonObjMain.getJSONArray("stampi");

                  int num_elem = jsonArray_stampi.length();

                   jsonObjMain.put( "nome","prova");
                   jsonObjMain.put( "lunghezza",22);
                   jsonObjMain.put( "larghezza", 10);
                   jsonObjMain.put( "altezza", 4);
                   jsonObjMain.put( "volume", 10.5);
                   jsonArray_stampi.put( jsonObjMain );
                   try {

                          FileWriter file = new FileWriter("c:\\test.json");
                          //file.write(jsonArray_stampi.);
                          file.write( JSON.stringify(jsonArray_stampi) );
                          file.flush();
                          file.close();

                      } catch (IOException e) {
                          e.printStackTrace();
                      }

              } catch (JSONException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              }   

          }           });

为什么不能正常工作?num_elem 变量总是 2..帮助我!

谢谢安德里亚

4

2 回答 2

0

看起来您正在尝试写入一个名为c:\\test.json. 尝试使用正确的 Android 方式写入带有openFileOutput. 例子在这里

String filename = "myfile";
String string = "Hello world!";
FileOutputStream outputStream;

try {
  outputStream = openFileOutput(filename, Context.MODE_PRIVATE);
  outputStream.write(string.getBytes());
  outputStream.close();
} catch (Exception e) {
  e.printStackTrace();
}
于 2013-09-06T15:43:47.637 回答
0

您必须创建一个新的 JSONObject,然后向其中添加新数据,然后附加到现有对象。

JSONObject jsonObjMain = new JSONObject(myjson_stampi); //Your existing object
JSONObject jO = new JSONObject(); //new Json Object
JSONArray jsonArray_stampi = jsonObjMain.getJSONArray("stampi"); //Array where you wish to append

//Add data
jO.put( "nome","prova");
jO.put( "lunghezza",22);
jO.put( "larghezza", 10);
jO.put( "altezza", 4);
jO.put( "volume", 10.5);

//Append
jsonArray_stampi.put(jO);

此外,您应该将完整的 jsonObject 写回文件。

file.write(JSON.stringify(jsonObjMain));
于 2013-09-06T13:31:40.940 回答