Приветствую! Я не практикую программирование на java и у меня не достаточно опыта чтобы выбрать библиотеку для работы с JSON (http://www.json.org/). Прошу помочь в выборе. Я пишу программу которая должна читать данные из JSON файла в utf-8 и должна быстро получать значения по ключу. Файл порядка 10000 записей, до 3х уровней вложенности с русскими ключами и значениями. Пример:
| Код | [{"pk": 1, "model": "rp.person", "fields": {"first_name": "Vova", "last_name": "Petrovich", "birthdate": "1999-01-01"}}, {"pk": 2, "model": "rp.person", "fields": {"first_name": "Jora", "last_name": "Kornev", "birthdate": "2222-02-02"}}, {"pk": 3, "model": "rp.person", "fields": {"first_name": "Vova", "last_name": "Petrovich", "birthdate": "1999-01-01"}}, {"pk": 4, "model": "rp.person", "fields": {"first_name": "Jora", "last_name": "Kornev", "birthdate": "2222-02-02"}}]
|
Хотелось бы получить от библиотеки такой use-case: >> json.get("pk").get(0) 1 >> json.get("fields").get(3).get("first_name") Jora
Пока остановился на json-simple (http://code.google.com/p/json-simple/), но она кажется не работает с юникодом.
| Код | import java.io.BufferedReader; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; import java.io.InputStreamReader;
import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.ss.usermodel.WorkbookFactory; import org.json.simple.JSONArray; import org.json.simple.JSONObject; import org.json.simple.parser.JSONParser;
public class Report { /** * @param args * @throws Exception */ public static void main(String[] args) throws Exception { String path = "s://STAND_ALONE_SOFTWARE//_DEV//workspace//report//src//"; InputStream fis = new FileInputStream(path + "json.txt"); InputStreamReader isr = new InputStreamReader(fis, "UTF-8"); BufferedReader br = new BufferedReader(isr); String line = ""; while (br.ready()){ line += br.readLine(); } JSONParser json = new JSONParser(); JSONArray array=(JSONArray)json.parse(line); JSONObject data = (JSONObject)array.get(0); InputStream inp = new FileInputStream(path + "test.xls"); Workbook wb = WorkbookFactory.create(inp); Sheet sheet = wb.getSheetAt(0); // Ищем шаблоны в ячейках for (int r = sheet.getFirstRowNum(); r < sheet.getLastRowNum(); r++){ Row current_row = sheet.getRow(r); if (current_row == null) continue; for (int c = current_row.getFirstCellNum(); c < current_row.getLastCellNum(); c++){ Cell current_cell = current_row.getCell(c); // Шаблон может находиться только в строке if (current_cell != null && current_cell.getCellType() == Cell.CELL_TYPE_STRING){ String text = current_cell.getStringCellValue(); int current_pos = 0; // Просматриваем инструкции в строке while (true){ int start_instruction = text.indexOf('{', current_pos); if (start_instruction == -1) break; current_pos += start_instruction + 1; int end_instruction = text.indexOf('}', current_pos); if (end_instruction == -1) throw new Exception("Unclosed instruction"); current_pos += end_instruction + 1; String instruction = text.substring(start_instruction, end_instruction + 1); String token = text.substring(start_instruction + 1, end_instruction); ...........................
} } } } ........................
|
Отваливается с юникодом:
| Код | Exception in thread "main" Unexpected character () at position 0. at org.json.simple.parser.Yylex.yylex(Unknown Source) at org.json.simple.parser.JSONParser.nextToken(Unknown Source) at org.json.simple.parser.JSONParser.parse(Unknown Source) at org.json.simple.parser.JSONParser.parse(Unknown Source) at org.json.simple.parser.JSONParser.parse(Unknown Source) at Report.main(Report.java:35)
|
|