Java Json解析listview自定义适配器
目前,我正在开发一个学习应用程序,它支持使用列表视图和json的动态菜单 我试图实现它,但无法读取节点JSON对象Java Json解析listview自定义适配器,java,json,Java,Json,目前,我正在开发一个学习应用程序,它支持使用列表视图和json的动态菜单 我试图实现它,但无法读取节点JSON对象 private void loadMainMenu() { try { FileInputStream inputStream = openFileInput(MainActivity.FILE_NAME); InputStreamReader inputStreamReader = new InputStreamReader(inputSt
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();
}
}
我需要它的列表视图将显示如下
Maths
Chemistry
Biology
Physics
History
尝试Jackson框架,您可以在这里找到: 它将为您处理文件和JSON数据结构。你只需要浏览一下。所需的输出由我的示例代码生成。该框架实际上可以做更多的事情,但在开始时,您可以坚持这一点 注意:可以在src/main/resources/下找到文件
test.json
,这里是maven示例项目中使用的文件。可以根据需要随意调整
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());
});
}
}
我的结果如下:
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":""}]
如果还有什么不清楚的地方,请询问。试试Jackson框架,您可以在这里找到: 它将为您处理文件和JSON数据结构。你只需要浏览一下。所需的输出由我的示例代码生成。该框架实际上可以做更多的事情,但在开始时,您可以坚持这一点 注意:可以在src/main/resources/下找到文件
test.json
,这里是maven示例项目中使用的文件。可以根据需要随意调整
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());
});
}
}
我的结果如下:
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":""}]
如果还有什么不清楚的地方,就问。这个问题难吗?这个问题难吗?