从Java代码Android到res / raw文件夹中的json-file

时间:2015-01-30 14:22:07

标签: java android json

我在尝试弄清楚如何从Android项目的assets文件夹中访问我的quiz.js(JSON文件)时遇到了一些麻烦。

这是我用来从JSON文件加载问题的方法:

public ArrayList<Question> loadQuestions() throws IOException, JSONException {
    ArrayList<Question> questions = new ArrayList<Question>();
    BufferedReader reader = null;
    try{
        // Open and read the file into a StringBuilder
        //InputStream in = mContext.openFileInput(mContext.getResources().getString(R.raw.quiz));
        //InputStream in = mContext.getResources().openRawResource(R.raw.quiz);
        InputStream in = mContext.getAssets().open("quiz.js");
        reader = new BufferedReader(new InputStreamReader(in));
        StringBuilder jsonString = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
            // Line breaks are omitted and irrelevant
            jsonString.append(line);
        }
        // Parse the JSON using JSONTokener
        JSONArray array = (JSONArray) new JSONTokener(jsonString.toString()).nextValue();

        // Build the array of crimes from JSONObjects
        for(int i=0; i< array.length(); i++){
            questions.add(new Question(array.getJSONObject(i)));
        }
    }
    catch (FileNotFoundException e){
        // Ignore this one; it happens when starting fresh
    }
    finally{
        if(reader != null){
            reader.close();
        }
    }
    return questions;
}

这是JSON文件json.js:

[
{
    "imageURI": "100",
    "question": "Hva betyr skiltet?",
    "answers": [
         "Farlig sving til venstre",
         "Farlig sving til høyre",
         "Blah..",
         "Blahh.."
     ],
    "correct": 0
},
{
    "imageURI": "101",
    "question": "Lorem ipsum dolor sit amet...",
    "answers": [
         "String 1",
         "String 2",
         "String 3",
         "String 4"
     ],
    "correct": 2
}

这是我尝试使用JSON数据的活动

public class QuizActivity extends Activity {

private ArrayList<Question> questions;
private QuizJSONSerializer mSerializer; 

private static final String TAG = "QuizActivity";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_quiz);


    // This is where i get the loadQuestions from
    mSerializer = new QuizJSONSerializer(getApplicationContext());

    try{
        questions = mSerializer.loadQuestions();
    }
    catch (Exception e){
        questions = new ArrayList<Question>();
        Log.e(TAG, "Error loading questions: ", e);
    }

    ImageView iv = (ImageView) findViewById(R.id.question_image);
    iv.setImageResource(R.drawable.mdpi_farlig_sving);

    TextView tv = (TextView) findViewById(R.id.question_text_view);
    if(questions == null){
        tv.setText("Something wrong with the array!");
    }
    else{
        tv.setText(questions.get(0).getQuestion());
    }

    Button b1 = (Button) findViewById(R.id.option_one);
    b1.setText("String 1");

    Button b2 = (Button) findViewById(R.id.option_two);
    b2.setText("String 2");

    Button b3 = (Button) findViewById(R.id.option_three);
    b3.setText("String 3");

    Button b4 = (Button) findViewById(R.id.option_four);
    b4.setText("String 4");
}

}

似乎永远不会加载JSON文件,因为我的异常代码总是运行。

JSON文件中是否缺少某些内容?

有什么好建议吗?

3 个答案:

答案 0 :(得分:1)

您应该创建资产文件夹并将文件放在那里。然后,您可以使用它来访问您的文件

InputStream is = context.getAssets().open("departments.json");

答案 1 :(得分:1)

为什么不使用assets文件夹将JSON捆绑到应用程序中?毕竟这是一种资产,而不是原始资源。

答案 2 :(得分:1)

使用以下方法创建资产文件夹:

InputStream input = context.getAssets().open("yourJSON.json");

您将找到您的文件。