Что было бы самым простым способом загрузить файл, содержащий JSON, в JSONObject.чтение JSONObject из файла JSON
разбор файла в JSONObject - без прерывистого объекта String (возможно, изнурительной память)
Это то, что у меня есть, но он бросает исключение:
public class AlphabetGestures {
private Map<Character,Vector<Point>> mMap;
public AlphabetGestures(String jsonFileName) throws JSONException {
mMap = new HashMap<Character,Vector<Point>>() ;
// parsing a file into a JSONObject - w/o the intermittent String object (arguably memory wasting)
File f = new File(jsonFileName); // making File object for .json file.
JSONObject masterJSON = new JSONObject(f.toString());
// giving .json file string to jsonvalue parser
JSONArray characters = masterJSON.names();
for (int c = 0; c < characters.length(); c++) {
// this loop will get each object from jsonArr
String character = characters.getString(c);
JSONArray pointsJSON = masterJSON.getJSONArray(character);
// this will get each element from jsonarray and make subarray for (A, B C ...)
Vector<Point> pointsVector = new Vector<Point>();
for (int i = 0; i < pointsJSON.length(); i++) {
JSONArray point = pointsJSON.getJSONArray(i);
pointsVector.add(new Point(point.getInt(0), point.getInt(1)));
}
mMap.put(character.charAt(0), pointsVector);
// put pointVector in mMap with key temp.
}
}
public void scribble(char letter, UiDevice uiDevice){
//System.out.println("here");
//AlphabetGestures Y = new AlphabetGestures() ;
Vector<Point> points = mMap.get(letter) ;
System.out.println("------------------");
System.out.println(points);
if(points!=null){
uiDevice.swipe(points.toArray(new Point[points.size()]), 5);
}
}
Output:- while running the test case
Error in testAccordion:
java.lang.NoSuchMethodError: ionmini.automate.AlphabetGestures.<init>
at ionmini.automate.AccordionDrag.testAccordion(AccordionDrag.java:33)
at java.lang.reflect.Method.invokeNative(Native Method)
at com.android.uiautomator.testrunner.UiAutomatorTestRunner.start(UiAutomatorTestRunner.java:160)
at com.android.uiautomator.testrunner.UiAutomatorTestRunner.run(UiAutomatorTestRunner.java:96)
at com.android.commands.uiautomator.RunTestCommand.run(RunTestCommand.java:91)
at com.android.commands.uiautomator.Launcher.main(Launcher.java:83)
at com.android.internal.os.RuntimeInit.nativeFinishInit(Native Method)
at com.android.internal.os.RuntimeInit.main(RuntimeInit.java:235)
at dalvik.system.NativeStart.main(Native Method)
* "без прерывистого объекта String (возможно, из-за потери памяти) *« ... Как вы думаете, вы собираетесь прочитать JSON из файла, который по определению является строкой текста, без использования String ? –
ссылка, которую вы указали, не использует прерывистый объект String (возможно, из-за потери памяти) .... idk, как они делают – user3281879
:: sigh :: A) Пожалуйста, прекратите говорить «возможно, потеря памяти». B) Оба ответа на дубликат читают весь файл в 'String' ... потому что вы читаете текстовые данные из файла. –