1

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)
user3281879
  • 33
  • 1
  • 2
  • 6
  • *"w/o the intermittent String object (arguably memory wasting)*" ... How do you suppose you're going to read the JSON from the file, which is by definition a string of text, without using String? – Brian Roach Feb 07 '14 at 23:11
  • the link you posted doesnot use the intermittent String object (arguably memory wasting)....idk how they r doing – user3281879 Feb 07 '14 at 23:16
  • ::sigh:: A) Please stop saying "arguably memory wasting". B) Both answers to the duplicate read the entire file into a `String` ... because that's how you read textual data from a file. – Brian Roach Feb 07 '14 at 23:19
  • i tried to follow the link but its giving me IOUtils error...i installed the jars from the code.google.com/p/json-simple – jars not found error – user3281879 Feb 07 '14 at 23:25
  • http://developer.android.com/reference/android/util/JsonReader.html or for API < 11 use jackson(library) streaming API – Selvin Feb 07 '14 at 23:27
  • ObjectMapper cannot be resolved to a type – user3281879 Feb 07 '14 at 23:50
  • Multiple markers at this line - The type org.codehaus.jackson.Versioned cannot be resolved. It is indirectly referenced from required .class files - The type org.codehaus.jackson.JsonProcessingException cannot be resolved. It is indirectly referenced from required .class files - The type org.codehaus.jackson.JsonParser cannot be resolved. It is indirectly referenced from required .class files - The type org.codehaus.jackson.JsonNode cannot be resolved. It is indirectly referenced from required .class files – user3281879 Feb 07 '14 at 23:56

1 Answers1

6

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.

Tony
  • 241
  • 1
  • 3
  • 12
  • So i dont need the following lines: SO i wont need the following lines: FileInputStream fis = new FileInputStream(f); byte[] data = new byte[(int)f.length()]; fis.read(data); fis.close(); – user3281879 Feb 07 '14 at 23:41
  • no need to create any inputstream... the mapper will parse the file – Tony Feb 07 '14 at 23:42
  • ok... new error: JsonNode cannot be resolved to a type – user3281879 Feb 07 '14 at 23:46
  • use JSONObject masterJSON = (JSONObject) mapper.readTree(from); instead – Tony Feb 07 '14 at 23:47
  • ObjectMapper cannot be resolved to a type – user3281879 Feb 07 '14 at 23:50
  • import org.codehaus.jackson.map.ObjectMapper or someotherpackage.ObjectMapper depends on your jackson package – Tony Feb 07 '14 at 23:52
  • Multiple markers at this line - The type org.codehaus.jackson.Versioned cannot be resolved. It is indirectly referenced from required .class files - The type org.codehaus.jackson.JsonProcessingException cannot be resolved. It is indirectly referenced from required .class files - The type org.codehaus.jackson.JsonParser cannot be resolved. It is indirectly referenced from required .class files - The type org.codehaus.jackson.JsonNode cannot be resolved. It is indirectly referenced from required .class files – – user3281879 Feb 07 '14 at 23:57
  • - The method readTree(JsonParser) from the type ObjectMapper refers to the missing type JsonNode - The type org.codehaus.jackson.ObjectCodec cannot be resolved. It is indirectly referenced from required .class files – user3281879 Feb 07 '14 at 23:57
  • JSONObject masterJSON = (JSONObject) mapper.readTree(f); – user3281879 Feb 07 '14 at 23:58
  • you really need to learn about dependencies in java... if the compiler says cannot be resolved, you need to import the missing class, which in this case is import org.codehaus.jackson.JsonNode; – Tony Feb 08 '14 at 00:05