将包含JSON的文件加载到JSONObject中的最简单方法是什么。
将文件解析为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)
答案 0 :(得分:1)
ObjectMapper可以轻松解决您的问题
ObjectMapper mapper = new ObjectMapper();
File from = new File("path to file");
JsonNode masterJSON = mapper.readTree(from);
您需要根据需要处理IOException。