gpt-3.5-turbo-0613· OpenAI
WildChat-4.8M (AI2)
java 读取json 文件demo
- User
java 读取json 文件demo
- Assistant
下面是一个示例代码来读取一个json文件:
import org.json.JSONArray; import org.json.JSONObject; import java.io.BufferedReader; import java.io.FileReader; public class ReadJsonFileDemo { public static void main(String[] args) { try { BufferedReader reader = new BufferedReader(new FileReader("input.json")); StringBuilder stringBuilder = new StringBuilder(); String line; while ((line = reader.readLine()) != null) { stringBuilder.append(line); } reader.close(); String jsonString = stringBuilder.toString(); JSONObject jsonObject = new JSONObject(jsonString); // 读取json中的字段 String name = jsonObject.getString("name"); int age = jsonObject.getInt("age"); System.out.println("Name: " + name); System.out.println("Age: " + age); // 读取json中的数组 JSONArray hobbiesArray = jsonObject.getJSONArray("hobbies"); System.out.println("Hobbies:"); for (int i = 0; i < hobbiesArray.length(); i++) { String hobby = hobbiesArray.getString(i); System.out.println("- " + hobby); } } catch (Exception e) { e.printStackTrace(); } } }
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.