简体   繁体   中英

JSON Parsing without using URL to read json file in android

I am using url to take the file for parsing. Now i want read the file which is in sdcard for parsing.How can i do this? here my code

reading file from url

    private static String url = "http://10.0.2.2/device1.json";
    JSONObject  json = jParser.getJSONFromUrl(url);
            devices = json.getJSONArray(TAG_DEVICES);

now i want do this from reading the file in sdcard

    java.io.File device = new java.io.File("/mnt/sdcard/device1.json");
    FileReader device_Fr = new FileReader(device);

next how to pass this file for parsing?

Use this code

read data from file into string & pass that to JSONObject.

BufferedReader reader = new BufferedReader(new FileReader(filePath));
    String line, results = "";
    while( ( line = reader.readLine() ) != null)
    {
        results += line;
    }
    reader.close();


JSONObject obj = new JSONObject(results);

One way to do it is to read the json and store it in a string. Then parse it like this:

   String jsonStr = // read from file
    JSONObject  json = new JSONObject( jsonStr);
    // parse
 

  devices = json.getJSONArray(TAG_DEVICES);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM