Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVA

Load local JSON

try
        {
            // Load File
            BufferedReader jsonReader = new BufferedReader(new InputStreamReader(getActivity().getResources().openRawResource(R.raw.province)));
            StringBuilder jsonBuilder = new StringBuilder();

            for (String line = null; (line = jsonReader.readLine()) != null;)
            {
                jsonBuilder.append(line).append("
");
            }

            // ParsJson
            JSONTokener tokener = new JSONTokener(jsonBuilder.toString());
            JSONArray jsonArray = new JSONArray(tokener);

            List<ProvinceList> fields = new ArrayList<>();
            for(int i = 0; i < jsonArray.length(); i++)
            {
                JSONObject object = jsonArray.getJSONObject(i);
                fields.add(new ProvinceList(object.getString("id"), object.getString("name")));

            }

            setDialogProvince(fields);

        }
        catch (FileNotFoundException e)
        {

        }
        catch (IOException e) {
            Log.e("JSON_ERROR", "ioError");
        }
        catch (JSONException e) {
            e.printStackTrace();
        }
 
PREVIOUS NEXT
Tagged: #Load #local #JSON
ADD COMMENT
Topic
Name
5+8 =