あなたは複雑なクエリを実行する必要がない場合は、私は非常によく、このようなGSONやJacksonとしてのlibを使ってAndroidアプリに統合以来JSONであなた件のデータを保存することをお勧めします。
あなたのアプリケーションを再構築したくない場合、すべての質問の変更時に再デプロイしてください。あなたは、アプリケーションの読み込み時に要求するjsonファイルを提供する小さなウェブサーバ(apache、nginx、tomcat)があると想像することができます。アプリがオンラインのときに質問をダウンロードしたり、キャッシュされたものを使用したりすることができます。
XMLは、このような用途には冗長な形式であり、多くの機能を持ちません....あなたの最後の質問への応答に
、あなたがそのようなあなたのコードを整理することができます。
/**
* SOF POST http://stackoverflow.com/posts/37078005
* @author Jean-Emmanuel
* @company RIZZE
*/
public class SOF_37078005 {
@Test
public void test() {
QuestionsBean questions = new QuestionsBean();
//fill you questions
QuestionBean b=buildQuestionExemple();
questions.add(b); // success
questions.add(b); //skipped
System.out.println(questions.toJson()); //toJson
}
private QuestionBean buildQuestionExemple() {
QuestionBean b= new QuestionBean();
b.title="What is the size of your boat?";
b.pictures.add("/res/images/boatSize.jpg");
b.order= 1;
return b;
}
public class QuestionsBean{
private List<QuestionBean> list = new ArrayList<QuestionBean>();
public QuestionsBean add(QuestionBean b){
if(b!=null && b.title!=null){
for(QuestionBean i : list){
if(i.title.compareToIgnoreCase(b.title)==0){
System.out.println("Question "+b.title+" already exists - skipped & not added");
return this;
}
}
System.out.println("Question "+b.title+" added");
list.add(b);
}
else{
System.out.println("Question was null/not added");
}
return this;
}
public String toJson() {
ObjectMapper m = new ObjectMapper();
m.configure(Feature.ALLOW_SINGLE_QUOTES, true);
String j = null;
try {
j= m.writeValueAsString(list);
} catch (JsonProcessingException e) {
e.printStackTrace();
System.out.println("JSON Format error:"+ e.getMessage());
}
return j;
}
}
public class QuestionBean{
private int order;
private String title;
private List<String> pictures= new ArrayList<String>(); //path to picture
private List<String> responseChoice = new ArrayList<String>(); //list of possible choices
public int getOrder() {
return order;
}
public void setOrder(int order) {
this.order = order;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public List<String> getPictures() {
return pictures;
}
public void setPictures(List<String> pictures) {
this.pictures = pictures;
}
public List<String> getResponseChoice() {
return responseChoice;
}
public void setResponseChoice(List<String> responseChoice) {
this.responseChoice = responseChoice;
}
}
}
コンソール出力
Question What is the size of your boat? added
Question What is the size of your boat? already exists - skipped & not added
[{"order":1,"title":"What is the size of your boat?","pictures":["/res/images/boatSize.jpg"],"responseChoice":[]}]
GIST: はあなたに私は「完全な作業コードを提供あなたのために作られました https://gist.github.com/jeorfevre/5d8cbf352784042c7a7b4975fc321466
結論として、JSONを使って作業するにはどうすればよいのですか: 1)jsonをビルドするためにBeanを作成してください(私の例を見てください) 2)jsonをビルドしてファイルに保存します 3)アンドロイドを使用してファイルからビーンにロードしてください(あなたはアンドロイドにあります) 4)ビーンを使ってフォームを作成します(jsonテキストファイルではありません):D
ありがとう、私はJSONを見ていきます。私は自分の投稿を編集しました。編集を読んで、あなたの答えに影響を与えるかどうか教えてください。 – exiibeat
は私の応答を更新しました。これは、Beanを埋める方法、json文字列を作成する方法を示すためです。 :)楽しむ – jeorfevre
ありがとう、非常にありがとう。私は家に帰るときにコードを調べてテストします。私がすべてをテストするとき、私はあなたの答えに印をつけます。 – exiibeat