Question

What would be the easiest way to load a file containing JSON into a JSONObject.

parsing a file into a JSONObject - w/o the intermittent String object (arguably memory wasting)

This is what I have, but it throws an exception:

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)
Was it helpful?

Solution

ObjectMapper can easily solve your problem

ObjectMapper mapper = new ObjectMapper();
File from = new File("path to file");
JsonNode masterJSON = mapper.readTree(from);

you need to handle IOException as needed.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top