【问题标题】:reading JSONObject from JSON file [duplicate]从 JSON 文件中读取 JSONObject [重复]
【发布时间】:2014-02-07 22:55:47
【问题描述】:

将包含 JSON 的文件加载到 JSONObject 中的最简单方法是什么。

将文件解析为 JSONObject - 没有间歇性字符串对象(可以说是浪费内存)

这是我所拥有的,但它会引发异常:

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)

【问题讨论】:

  • "w/o 间歇性 String 对象(可以说是浪费内存)" ...您认为您将如何从文件中读取 JSON,即根据定义一个文本字符串,不使用字符串?
  • 您发布的链接不使用间歇性 String 对象(可以说是浪费内存)....idk 他们是怎么做的
  • ::sigh:: A) 请不要再说“可以说是在浪费内存”。 B)重复的两个答案都将整个文件读入String ...因为这就是您从文件中读取文本数据的方式。
  • 我试图点击链接,但它给了我 IOUtils 错误...我从 code.google.com/p/json-simple 安装了 jars – jars not found 错误

标签: java android json android-uiautomator


【解决方案1】:

ObjectMapper 可以轻松解决您的问题

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

您需要根据需要处理 IOException。

【讨论】:

  • 所以我不需要以下几行:所以我不需要以下几行:FileInputStream fis = new FileInputStream(f);字节[] 数据 = 新字节[(int)f.length()]; fis.read(数据); fis.close();
  • 无需创建任何输入流...映射器将解析文件
  • ok...新错误:JsonNode 无法解析为类型
  • 使用 JSONObject masterJSON = (JSONObject) mapper.readTree(from);而是
  • ObjectMapper 无法解析为类型
猜你喜欢
  • 1970-01-01
  • 2018-01-07
  • 1970-01-01
  • 2020-08-15
  • 1970-01-01
  • 2019-02-14
  • 2019-12-24
  • 1970-01-01
  • 2019-12-27
相关资源
最近更新 更多