package nj.zb.kb22;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
public class DataProcessJson {
public static void main(String[] args) {
String str = "{\n" +
" \"employees\": [\n" +
" {\n" +
" \"firstName\": \"John\",\n" +
" \"lastName\": \"Doe\",\n" +
" \"email\": \"john.doe@example.com\",\n" +
" \"age\": 30,\n" +
" \"address\": {\n" +
" \"street\": \"123 Main St\",\n" +
" \"city\": \"New York\",\n" +
" \"state\": \"NY\",\n" +
" \"postalCode\": \"10001\"\n" +
" },\n" +
" \"skills\": [\"Java\", \"Python\", \"SQL\"]\n" +
" },\n" +
" {\n" +
" \"firstName\": \"Jane\",\n" +
" \"lastName\": \"Smith\",\n" +
" \"email\": \"jane.smith@example.com\",\n" +
" \"age\": 28,\n" +
" \"address\": {\n" +
" \"street\": \"456 Elm St\",\n" +
" \"city\": \"San Francisco\",\n" +
" \"state\": \"CA\",\n" +
" \"postalCode\": \"94101\"\n" +
" },\n" +
" \"skills\": [\"JavaScript\", \"HTML\", \"CSS\"]\n" +
" }\n" +
" ],\n" +
" \"company\": \"ABC Corporation\",\n" +
" \"website\": \"www.example.com\"\n" +
"}";
StringBuilder stringBuilder = new StringBuilder();
BufferedWriter bufferedWriter = null;
try {
JSONObject jsonObject = new JSONObject(str);
JSONArray employees = jsonObject.getJSONArray("employees");
for (int i = 0; i < employees.length(); i++) {
JSONObject jsonObject1 = employees.getJSONObject(i);
String firstname = jsonObject1.getString("firstName");
StringBuilder append = stringBuilder.append(firstname).append("\\n\\n");
//System.out.println(firstname);
bufferedWriter = new BufferedWriter(new FileWriter("D:\\KB22f\\nj\\zb\\javaspark\\.idea\\text\\text.txt"));
bufferedWriter.write(String.valueOf(append));
}
System.out.println(stringBuilder);
} catch (JSONException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
if (bufferedWriter != null) {
try {
bufferedWriter.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}//main
}