私はかなりの間、この問題を解決しようとしていましたが、私はもう取るべきルートを持っていないようです。jsonは有効ではありません、引用符とバックスラッシュ
私はreplace( "\"、 "")で取り除くことができたバックスラッシュをいくつか持っていましたが、私はまだこのような引用符をいくつか持っています---> "私の地図の冒頭にあります。それはそこに取得する方法任意のアイデアを持っている
ここに私のコードです:ここで
public void actionPerformed(ActionEvent e) {
JsonObject obj = new JsonObject();
JsonArray ingredList = new JsonArray();
Map<String, String> ingredientAsMap = new HashMap<>();
JsonArray prepTime = new JsonArray();
Map<String, String> prepTimeAsMap = new HashMap<>();
JsonArray cookTime = new JsonArray();
Map<String, String> cookTimeAsMap = new HashMap<>();
obj.addProperty("Recipe Name", textField1.getText().toString());
for (int r = 0; r < model.getRowCount(); r++) {
ingredientAsMap.put("Ingredient", model.getValueAt(r, 0).toString());
ingredientAsMap.put("Measure", model.getValueAt(r, 1).toString());
ingredientAsMap.put("Quantity", model.getValueAt(r, 2).toString());
ingredList.add(String.valueOf(ingredientAsMap));
}
obj.addProperty("Ingredients Lists", String.valueOf(ingredList));
obj.addProperty("Preparation", editorPane1.getText().toString());
prepTimeAsMap.put("Hours", spinner3.getValue().toString());
prepTimeAsMap.put("Mins", spinner2.getValue().toString());
prepTime.add(String.valueOf(prepTimeAsMap));
obj.addProperty("Preparation Time", String.valueOf(prepTime));
cookTimeAsMap.put("Hours", spinner1.getValue().toString());
cookTimeAsMap.put("Mins", spinner4.getValue().toString());
cookTime.add(String.valueOf(cookTimeAsMap));
obj.addProperty("Cooking Time", String.valueOf(cookTime));
Gson gson = new GsonBuilder().setPrettyPrinting().disableHtmlEscaping().create();
String json = gson.toJson(obj).replace("\\", "");
try{
FileWriter writer = new FileWriter(System.getProperty("user.dir") + "\\" + textField1.getText().toString() + ".json");
BufferedWriter bw = new BufferedWriter(writer);
bw.write(json);
bw.close();
} catch (IOException e1) {
e1.printStackTrace();
}
System.out.println(json);
//System.out.println(System.getProperty("user.dir") + "\\" + textField1.getText().toString() + ".json");
}
は私のフォームが満たされたときに私が手に出力されます:
{
"Recipe Name": "Test",
"Ingredients Lists": "["{Ingredient=Ingredient0, Measure=ml, Quantity=0}","{Ingredient=Ingredient1, Measure=ml, Quantity=0}"]",
"Preparation": "This is a test.",
"Preparation Time": "["{Hours=0, Mins=0}"]",
"Cooking Time": "["{Hours=0, Mins=0}"]"
}
はお知らせ」で[前成分リスト、調製Ti私と調理時間。どうすればそれらを取り除くことができますか?
ありがとうございます!
「add(String property、JsonElement value)」を使用してください。 –
Ah!あなたは天才!私はダブル/トリプルJsonに変換していた... –
ようこそ:)申し訳ありません、長く、詳細な答え。あなたは[答えを受け入れる](//stackoverflow.com/help/someone-answers)によってあなたの感謝を示すかもしれません。 –