简体   繁体   中英

Json parse listview custom adapter

Currently I'm developing a Learning App that supports a dynamic Menu using list view and json.

I tried to implement it but I can't read the node JSON Objects.

private void loadMainMenu() {
    try {
        FileInputStream inputStream = openFileInput(MainActivity.FILE_NAME);
        InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
        StringBuilder builder = new StringBuilder();
        String line;
        while ((line = bufferedReader.readLine()) != null) {
            builder.append(line);
        }
        JSONObject jsonObj = new JSONObject(builder.toString());
        String obj = jsonObj.getString("rootNode");
        JSONArray jsonArray = new JSONArray(obj);
        for (int j = 0; j < obj.length(); j++) {
            TitleModel title = new TitleModel(jsonObj.getJSONObject(String.valueOf(j)).toString());
            titleArrayList.add(title);
            titleAdapter = new TitleAdapter(this, titleArrayList);
        }
    } catch (IOException e) {
        e.printStackTrace();
    } catch (JSONException e) {
        e.printStackTrace();
    }
}

and the json files is this.

{"Maths":[{"Part":"ክፍል 1","url":""}],"Chemistry":[{"Part":"ክፍል 1","url":""}],"Biology":[{"Part":"ክፍል 1","url":""}],"Physics":[{"Part":"ክፍል 1","url":""}],"History ":[{"Part":"ክፍል 1","url":""}]}

What I need it that the list view will show like this.

Maths  
Chemistry  
Biology  
Physics  
History 

Try the Jackson Framework, which you can find here: https://github.com/FasterXML/jackson

It will take care of file handling and JSON data structure for you. You only need to navigate through. The desired output is produced by my sample code. The framework actually can do a lot more, but for the beginning, you can stick with this.

Note: the file test.json is to be found under src/main/resources/, as used in a maven sample project here. Feel free to adapt this as desired.

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

public class App {

    public static void main(String[] args) {

        ObjectMapper mapper = new ObjectMapper();
        JsonNode node = null;

        try {
            node = mapper.readValue(new File(App.class.getClassLoader().getResource("test.json").getPath()), JsonNode.class);
        } catch (Exception e) {
            // TODO -- handle exception
            e.printStackTrace();
        }

        node.fieldNames().forEachRemaining(System.out::println);

        System.out.println(" --- ALTERNATIVELY ---");

        node.fields().forEachRemaining( currDiscipline -> {
            System.out.println("Menu item: " + currDiscipline.getKey() + " with " + currDiscipline.getValue());
        });
    }
}

My result looks as follows:

Maths
Chemistry
Biology
Physics
History 
 --- ALTERNATIVELY ---
Menu item: Maths with [{"Part":"ክፍል 1","url":""}]
Menu item: Chemistry with [{"Part":"ክፍል 1","url":""}]
Menu item: Biology with [{"Part":"ክፍል 1","url":""}]
Menu item: Physics with [{"Part":"ክፍል 1","url":""}]
Menu item: History  with [{"Part":"ክፍል 1","url":""}]

If anything remains unclear, just ask.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM