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 ?
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.
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.
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”.
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();
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With