在Json文件中附加数据

时间:2013-09-06 13:05:08

标签: android json append

我有这种类型的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 always..help me!

Andrea

2 个答案:

答案 0 :(得分: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));

答案 1 :(得分:0)

看起来您正在尝试写入名为c:\\test.json的文件。尝试使用正确的Android方式写入openFileOutput的文件。示例是here

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();
}