解析字符串并创建json格式然后保存到textfile

时间:2015-12-16 04:00:43

标签: java android arrays json string

我想创建一个json文本并将其保存到textfile。到目前为止,这是我编辑过的代码。我希望jsonFormat.txt中的输出必须包含这些字符串{" Fname":" John low"," Mname":" Bajud" ," Lname":" Saddy"}。

        File myFile = new File("/sdcard/jsonFormat.txt");
        myFile.createNewFile();
        String delim = "\\|";
        String[] temp;
        String temp2;
        String data ="|Fname:John low|Mname:Bajud|Lname:Saddy|";

        data = data.substring(1, data.length() - 1);
        temp2 = data.split(delim);

        JSONObject jsonObject = new JSONObject();

         for(int index=0;index < temp2.length() ;index++){
             String[] sub_temp2 = temp2[index].split(":");
             jsonObject.put(sub_temp2[0],sub_temp2[1]);
         }  

         FileOutputStream fOuts = new FileOutputStream(myFile);
         OutputStreamWriter myOutWriter = new OutputStreamWriter(fOuts);
         myOutWriter.append(jsonObject.toString());
         myOutWriter.close();
         fOuts.close();

我决定解决我的问题,在经历了挣扎之后我找到了解决办法:)这是我对上述问题的解决方案:)

 try {
        File myFile = new File("/sdcard/jsonFormat.txt");
        FileOutputStream fOuts = new FileOutputStream(myFile);
        myFile.createNewFile();

        StringBuilder builder = new StringBuilder();
        StringBuilder builder2 = new StringBuilder();
        String delim = "\\|";
        String delim2 = "\\:";
        String[] temp;
        String[] temp2;
        String[] sub;
        String data = "|Fname:James Bryan|Mname:Baguio|Lname:Juventud|Age:21|Gender:Male|Fname:James Bryan|Mname:Baguio|Lname:Juventud|Age:21|Gender:Male|";

        data = data.substring(1, data.length() - 1);
        temp2 = data.split(delim);
        //System.out.print(temp2[1]+"\n"); = Fname:James Bryan
        builder.append("{");

        int k=1;
        for(int i=0;i<temp2.length;i++){
            //System.out.print(temp2[i]+"\n");
            sub = temp2[i].split(delim2);
            for(int x=0;x<sub.length;x++){
                builder.append("\"");
                builder.append(sub[x]);
                builder.append("\"");

                if(k<2){
                    builder.append(":");
                    k++;
                }
                else
                    k--;
            }
            builder.append(",");
        }
        builder.append("}");

        String newData = builder.substring(0, builder.length()-2);
        builder2.append(newData);
        builder2.append("}");
        //System.out.println(builder2.toString());
        OutputStreamWriter myOutWriter = new OutputStreamWriter(fOuts);
        myOutWriter.append(builder2);
        myOutWriter.close();
        fOuts.close();

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

    }

输出:JSON String textfile

 {"Fname": "James Bryan","Mname": "Baguio","Lname": "Juventud","Age": "21","Gender": "Male","Fname": "James Bryan","Mname": "Baguio","Lname": "Juventud","Age": "21","Gender": "Male"}

3 个答案:

答案 0 :(得分:2)

您的代码

temp2 = data.split(delim);

无效,因为delim错误

尝试

String delim = "\\|";

temp2应该是一个String数组;

答案 1 :(得分:1)

首先,代码有多个问题。

  1. temp2 需要是一个String数组
  2. for循环应限制在“temp2.​​length”而不是length()
  3. 您使用“[|]”的分隔符不正确。你可以只在管道上拆分“|”没有试图逃脱管道。
  4. 我建议重新访问代码并了解它在做什么,并尝试一次编译和解决问题。

答案 2 :(得分:1)

我没有告诉如何将字符串转换为json但是你试试这个

public void saveContentToDisk(Context context,
            String filename, String json) {
String data = null;
if (json != null && !json.isEmpty()) {
        Gson gson = new Gson();
        data = gson.fromJson(json, new TypeToken<String>() {
        }.getType());    
    }
        try {
            FileWriter out = new FileWriter(new File(
                    context.getExternalCacheDir(), filename));
            out.write(data);
            out.close();
            if (Util.IS_DEBUG_LOGGABLE) {
                Log.d(TAG, "saved data to disk " + filename);
            }
        } catch (IOException e) {
            Log.e(TAG, "Error saving data to disk, " + e.getMessage());
        }
    }