Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a json file from raw folder?

I want to use my user.json which is in my raw folder to get a new File :

// read from file, convert it to user class
User user = mapper.readValue(new File(**R.raw.user**), User.class);

I found that InputStream can do it :

InputStream ins = res.openRawResource(
                        getResources().getIdentifier("raw/user",
                                "raw", getPackageName()));

Is there a better way to do it, directly with my json file ID ?

like image 258
machichiotte Avatar asked Sep 11 '15 08:09

machichiotte


People also ask

How do I get a json file?

Steps to open JSON files on Web browser (Chrome, Mozilla)Open the Web store on your web browser using the apps option menu or directly using this link. Here, type JSON View in search bar under the Extensions category. You will get the various extensions similar to JSON View to open the JSON format files.

What is a raw JSON file?

Raw JSON text is the format Minecraft uses to send and display rich text to players. It can also be sent by players themselves using commands and data packs. Raw JSON text is written in JSON, a human-readable data format.

Can I download a JSON file?

Pre-filled JSON can be downloaded after logging into the e-filing portal through: 'My Account -> 'Download Pre-Filled for AY 2022-23' and is imported to the utility for pre-filling the personal and the other opened information. Next Attach the pre-filled file of JSON via the system and Tap on “proceed”.


2 Answers

InputStream is = getResources().openRawResource(R.raw.json_file);
Writer writer = new StringWriter();
char[] buffer = new char[1024];
try {
    Reader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
    int n;
    while ((n = reader.read(buffer)) != -1) {
        writer.write(buffer, 0, n);
    }
} finally {
    is.close();
}

String jsonString = writer.toString();
like image 153
Nooruddin Lakhani Avatar answered Oct 21 '22 05:10

Nooruddin Lakhani


ObjectMapper.readValue also take InputStream as source . Get InputStream using openRawResource method from json file and pass it to readValue :

InputStream in = getResources().openRawResource(R.raw.user);
User user = mapper.readValue(in, User.class);
like image 5
ρяσѕρєя K Avatar answered Oct 21 '22 05:10

ρяσѕρєя K