Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何将Json数组内容获取到ArrayList<;字符串>;在爪哇?_Android_Arrays_Json_Arraylist - Fatal编程技术网

Android 如何将Json数组内容获取到ArrayList<;字符串>;在爪哇?

Android 如何将Json数组内容获取到ArrayList<;字符串>;在爪哇?,android,arrays,json,arraylist,Android,Arrays,Json,Arraylist,我在android“资产”文件夹中有Json文件。Json文件有五个以上的数组如何存储到Java ArrayList或如何直接将Json数组获取到forLoop。试试这种方法 public static String AssetJSONFile (String filename, Context context) throws IOException { AssetManager manager = context.getAssets(); InputStream file =

我在android“资产”文件夹中有Json文件。Json文件有五个以上的数组如何存储到Java ArrayList或如何直接将Json数组获取到forLoop。

试试这种方法

 public static String AssetJSONFile (String filename, Context context) throws IOException {
    AssetManager manager = context.getAssets();
    InputStream file = manager.open(filename);
    byte[] formArray = new byte[file.available()];
    file.read(formArray);
    file.close();

    return new String(formArray);
  }         

   public void getJson()
   { 
    ArrayList<HashMap<String, String>> formList = new ArrayList<HashMap<String, String>>();
    Context context = null;
    HashMap<String, String> hashMap;

    try {
        String jsonLocation = AssetJSONFile("yourjson.json", context);
        String id;
        String question;

        JSONArray JsonArray = jsonObject.getJSONArray("arjuna");
        {
              for (int k = 0; k < JsonArray.length(); k++) 
             {  
              JSONObject jsonObject = JsonArray.getJSONObject(k);

              id= JSONObject.getString("id");
              question= JSONObject.getString("question");

              hashMap = new HashMap<String, String>();
              hashMap.put("id", id);
              hashMap.put("question", question);

              formList.add(hashMap); 
            }
        }

    } catch (IOException e) {
        e.printStackTrace();
    } catch (JSONException e) {
        e.printStackTrace();
    }
 }
publicstaticstringassetjsonfile(字符串文件名、上下文)抛出IOException{
AssetManager=context.getAssets();
InputStream file=manager.open(文件名);
byte[]formArray=新字节[file.available()];
文件读取(formArray);
file.close();
返回新字符串(formArray);
}         
public void getJson()
{ 
ArrayList formList=新的ArrayList();
Context=null;
HashMap;
试一试{
字符串jsonLocation=AssetJSONFile(“yourjson.json”,context);
字符串id;
字符串问题;
JSONArray JSONArray=jsonObject.getJSONArray(“阿诸那”);
{
for(int k=0;k
试试看{
InputStream InputStream=getAssets().open(“json.txt”);
字符串s=convertStreamToString(inputStream);
JSONObject对象=新的JSONObject();
JSONArray JSONArray=object.getJSONArray(“arjuna”);
HashMap HashMap=null;
ArrayList ArrayList=新的ArrayList();
for(int i=0;i
你试过什么吗?。把它放在这里。就像从TextView中的Json数组生成的随机问题一样
try {
            InputStream inputStream = getAssets().open("json.txt");
            String s = convertStreamToString(inputStream);
            JSONObject object = new JSONObject();
            JSONArray jsonArray = object.getJSONArray("arjuna");
            HashMap hashMap = null;
            ArrayList<HashMap> arrayList = new ArrayList<HashMap>();
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject obj = (JSONObject) jsonArray.get(i);
                int id = obj.getInt("id");
                String question = obj.getString("question");
                hashMap = new HashMap();
                hashMap.put("id", id);
                hashMap.put("question", question);
                arrayList.add(hashMap);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }

private String convertStreamToString(InputStream is) {
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        StringBuilder sb = new StringBuilder();

        String line = null;
        try {
            while ((line = reader.readLine()) != null) {
                sb.append(line).append('\n');
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return sb.toString();
    }