I would like to create a JSON file in the internal storage of the phone, to store data.
I want to be able to add objects ("configX") to the file and then read the data.
It should look something like this:
{
"config1": {
"component1": "url",
"component2": "url",
"component3": "url"
},
"config2": {
"component1": "url",
"component2": "url",
"component3": "url"
}
}
I can create a JSON file like this :
public void saveToJson(){
JSONObject json = new JSONObject();
try {
json.put("component1", "url");
json.put("component2", "url");
String jsonString = json.toString();
FileOutputStream fos = this.openFileOutput("jsonfile", Context.MODE_PRIVATE);
fos.write(jsonString.getBytes());
fos.close();
Log.d("JSON" , json.toString());
} catch (IOException | JSONException e) {
e.printStackTrace();
}
}
But how to put the components in the config object ? And how to retrieve the data ?
EDIT 1 :
https://stackoverflow.com/a/62474912/11652860
Thanks for the very detailed answer, I'm doing something wrong. I have an Activity where I put and save data to the json file:
public class Data {
private Map<String, Map<String, String>> map;
public Data() {
}
public Data(Map<String, Map<String, String>> map) {
this.map = map;
}
public Map<String, Map<String, String>> getMap() {
return map;
}
public void setMap(Map<String, Map<String, String>> map) {
this.map = map;
}
}
Map<String, String> config1 = new HashMap<>();
config1.put("component1", "url1");
config1.put("component2", "url1");
config1.put("component3", "url1");
Map<String, Map<String, String>> map = new HashMap<>();
map.put("config1", config1);
Data data = new Data(map);
Gson gson = new Gson();
String json = gson.toJson(data);
FileOutputStream fos = null;
try {
fos = webViewActivity.this.openFileOutput("jsonfile", Context.MODE_PRIVATE);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
fos.write(json.getBytes());
} catch (IOException e) {
e.printStackTrace();
}
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
And a fragment where I load the data :
public class Data {
private Map<String, Map<String, String>> map;
public Data() {
}
public Data(Map<String, Map<String, String>> map) {
this.map = map;
}
public Map<String, Map<String, String>> getMap() {
return map;
}
public void setMap(Map<String, Map<String, String>> map) {
this.map = map;
}
}
public void load(){
FileInputStream fis = null;
try {
fis = getContext().openFileInput("jsonfile.txt");
InputStreamReader isr = new InputStreamReader(fis);
BufferedReader br = new BufferedReader(isr);
StringBuilder sb = new StringBuilder();
String text;
while ((text = br.readLine()) != null){
sb.append(text).append("\n");
Gson gson = new Gson();
String json = gson.toJson(text);
Data data = gson.fromJson(json, Data.class);
String url = data.getMap().get("config1").get("component1");
frameTV.setText(url);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fis != null){
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
The saving and loading parts must be wrong, but they worked for getting text out a text file
EDIT 2 :
I found the problem, I wasn't loading and saving properly :
SAVING:
String filename = "jsonfile.txt";
FileOutputStream outputStream;
try {
outputStream = openFileOutput(filename, Context.MODE_PRIVATE);
outputStream.write(json.getBytes());
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
LOADING :
FileInputStream fis = getContext().openFileInput("jsonfile.txt");
InputStreamReader isr = new InputStreamReader(fis);
BufferedReader bufferedReader = new BufferedReader(isr);
StringBuilder sb = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
sb.append(line);
}
String json = sb.toString();
Gson gson = new Gson();
Data data = gson.fromJson(json, Data.class);
String priceURL = data.getMap().get("config1").get("url1");
EDIT 3 :
My problem now is that I need to create the file once and then check if the file exists, if it does I need to check if config1 exists if it doesn't I need to put config in the file.
But I can't check if config1 exists because I get : java.lang.NullPointerException: Attempt to invoke virtual method 'java.util.Map com.a.app.ui.app.appFragment$Data.getMap()
I check if it exists by doing :
Boolean configTest = data.getMap().containsKey("config1");
if(!configTest){}
How can I create the file and check the data without getting a NullPointerException ?
Thank you for helping me !
Google's Gson library will be helpful in this case.
Add dependency for Google Gson in your radle file.
dependencies {
implementation 'com.google.code.gson:gson:2.8.6'
}
Create a class for your data container
public class Data {
private Map<String, Map<String, String>> map;
public Data() {
}
public Data(Map<String, Map<String, String>> map) {
this.map = map;
}
public Map<String, Map<String, String>> getMap() {
return map;
}
public void setMap(Map<String, Map<String, String>> map) {
this.map = map;
}
}
Add data to your class
Map<String, String> config1 = new HashMap<>();
config1.put("component1", "url1");
config1.put("component2", "url1");
config1.put("component3", "url1");
Map<String, String> config2 = new HashMap<>();
config2.put("component1", "url1");
config2.put("component2", "url1");
config2.put("component3", "url1");
Map<String, Map<String, String>> map = new HashMap<>();
map.put("config1", config1);
map.put("config2", config2);
Data data = new Data(map);
Get gson from data class
Gson gson = new Gson();
String json = gson.toJson(data);
You can now save this json in a file in a text format.
Now when reading, load the content of the text file in a String say 'jsonString'.
Deserialize the jsonString to Java Object
Data data = gson.fromJson(json, Data.class);
Access configurations
String url = data.getMap().get("config1").get("component1");
Add new configurations
Map<String, String> config3 = new HashMap<>();
config3.put("component1", "url1");
config3.put("component2", "url1");
config3.put("component3", "url1");
data.getMap().put("config3", config3);
Follow again these steps to save configs
Or You can manually edit the text file to add configs according to the predefined format.
{
"maps":{
"config2":{
"component1":"url1",
"component2":"url1",
"component3":"url1"
},
"config1":{
"component1":"url1",
"component2":"url1",
"component3":"url1"
}
}
}
This is how you create multiple Objects in a single JSON object:
//Creating first Object
JSONObject config1 = new JSONObject();
try {
json.put("component1", "url");
json.put("component2", "url");
json.put("component2", "url");
}
catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//Creating second object
JSONObject config2 = new JSONObject();
try {
json.put("component1", "url");
json.put("component2", "url");
json.put("component2", "url");
}
catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
JSONObject finalJSON = new JSONObject();
try {
//Adding both objects in one single object
json.put("config1", config1);
json.put("config2", config2);
String jsonString = finalJSON.toString();
FileOutputStream fos = this.openFileOutput("jsonfile", Context.MODE_PRIVATE);
fos.write(jsonString.getBytes());
fos.close();
Log.d("JSON" , json.toString());
} catch (IOException | JSONException e) {
e.printStackTrace();
}
This will give you the desired output. Also, if in case you want to make any object an array, you can use JSONArray for that.
Please consider using https://github.com/google/gson. You will be working with class instance rather than with JSONObject. Much more convenient.
Just to give you the idea of what you can do:
public class TestClass {
private final Map<String, String> config1;
private final Map<String, String> config2;
public TestClass(Map<String, String> config1, Map<String, String> config2) {
this.config1 = config1;
this.config2 = config2;
}
}
Gson gson = new GsonBuilder().setPrettyPrinting().create();
Map<String, String> config1 = new HashMap<String, String>();
config1.put("hello1.1", "world1.1");
config1.put("hello1.2", "world1.2");
Map<String, String> config2 = new HashMap<String, String>();
config2.put("hello2.1", "world2.1");
config2.put("hello2.2", "world2.2");
TestClass testClass = new TestClass(config1, config2);
Log.d("zzz", gson.toJson(testClass));
The above prints:
{
"config1": {
"hello1.1": "world1.1",
"hello1.2": "world1.2"
},
"config2": {
"hello2.1": "world2.1",
"hello2.2": "world2.2"
}
}
You can go back and force between json string and the entity itself. To edit, you only need to work with object - natural and convenient way.
My code:
private List<Day> readDays(File file) {
List<Day> days = new ArrayList<>();
try {
ObjectInputStream in = new ObjectInputStream(new FileInputStream(file));
days.addAll((List<Day>) in.readObject());
} catch (IOException | ClassNotFoundException e) {
Logger.logError(LOG_TAG, e);
}
return days;
}
Unchecked cast problem in this code
days.addAll((List<Day>) in.readObject());
And this is a problem, in some cases the app crashes.
if your problem is cast object; you can define a convertor to convert your object to your class and handle exceptions.
if your stream return json string, You can use ObejctMapper and convert json string to your class as follow method by using jackson library:
//create ObjectMapper instance
ObjectMapper objectMapper = new ObjectMapper();
//convert json string to object
Day day = objectMapper.readValue(jsonData, Day.class);
// use day class now
so converting object, depend on your file data format.
I am using Jackson and am able to get a JSONObject. I need to be able to convert this JSONObject to its json form. Meaning, the object that is represented by this JSONObject's son string.
Something like:
JsonObject object = ...;
object.toJsonString();
A simple Google search surprisingly didn't turn up many response and I am unable to see how to do it on my own.
Any ideas?
Try,
JSONObject object = ...;
ObjectMapper mapper = new ObjectMapper();
String jsonString = mapper.writeValueAsString(object);
StringWriter out = new StringWriter();
object.writeJSONString(out);
String jsonText = out.toString();
System.out.print(jsonText);
If you need to bridge the 2 APIs you can create a custom StdSerializer.
More on custom serializers:
https://www.baeldung.com/jackson-custom-serialization
private static class JSONObjectSerializer extends StdSerializer<JSONObject> {
JSONObjectSerializer(){
this(null);
}
JSONObjectSerializer(Class<JSONObject> t) {
super(t);
}
#Override public void serialize(JSONObject value, JsonGenerator gen, SerializerProvider provider) throws IOException {
gen.writeStartObject();
value.keys().forEachRemaining(key-> {
try {
gen.writeStringField(key,value.getString(key));
} catch (IOException ex){
throw new RuntimeException("Encountered an error serializing the JSONObject.",ex);
}
});
gen.writeEndObject();
}
private static SimpleModule toModule(){
return new SimpleModule().addSerializer(JSONObject.class, new JSONObjectSerializer());
}
}
//.......
ObjectWriter writer = new ObjectMapper()
.registerModule(JSONObjectSerializer.toModule())
.writerWithDefaultPrettyPrinter();
//.......
try {
s = w.writeValueAsString(v);// v being your JSONObject or parent
} catch (JsonProcessingException ex) {
throw new RuntimeException("Unable to write object to json.", ex);
}
I m trying to parse a JSON file and store it in an list. I m getting this error :
com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.util.ArrayList out of START_OBJECT token
Here is my JSON file
{ "budgetList":[
{
"label":"Salary Tim",
"category":"Monthly Income",
"real":1590,
"estimated":1590,
"date":"",
"month":"",
"year":"",
"type":"Income"
},
{
"label":"Salary Tom",
"category":"Monthly Income",
"real":1540,
"estimated":1540,
"date":"",
"month":"",
"year":"",
"type":"Income"
}
]
}
Here is my code
Budget :
public class Budget {
private String label;
private String category;
private int real;
private int estimated;
private Date date;
private int year;
private String type;
....
....
}
My service :
List<Budget> budgets = objectMapper.readValue(new File("src/main/resources/json/new_exercise.json"), TypeFactory.defaultInstance().constructCollectionType(List.class,
Budget.class));
Where am I wrong?
Thanks in advance.
ANSWER FOUND
Code is
ObjectMapper objectMapper = new ObjectMapper();
List<Budget> budgets = null;
JsonNode node = objectMapper.readTree(new File("src/main/resources/json/new_exercise.json"));
node = node.get("budgetList");
TypeReference<List<Budget>> typeRef = new TypeReference<List<Budget>>(){};
budgets = objectMapper.readValue(node.traverse(), typeRef);
can you, use GSON library? Is very simple
Reader reader = new InputStreamReader(new
FileInputStream("/opt/file.json"));
Gson gson = gsonBuilder.create();
List listBudget = gson.fromJson(reader, new
TypeToken>() {}.getType());
I think that the only problem is when Date and integer parser when is empty. but you can register adapters like:
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.registerTypeAdapter(Date.class, new JsonDeserializer<Date>() {
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
public Date deserialize(final JsonElement json, final Type typeOfT, final JsonDeserializationContext context)
throws JsonParseException {
try {
return df.parse(json.getAsString());
} catch (ParseException e) {
return null;
}
}
});
Reader reader = new InputStreamReader(new
FileInputStream("/opt/file.json"));
Gson gson = gsonBuilder.create();
List listBudget = gson.fromJson(reader, new
TypeToken>() {}.getType());
It works for you?, and dont forget to validate you json. "
I am using json-rpc-1.0.jar.Below is my code. I need to convert InputStream object into JSON since the response is in JSON.
I did verify the json response obtained from Zappos API. It is valid.
PrintWriter out = resp.getWriter();
String jsonString = null;
URL url = new URL("http://api.zappos.com/Search?term=boots&key=my_key");
InputStream inputStream = url.openConnection().getInputStream();
resp.setContentType("application/json");
JSONSerializer jsonSerializer = new JSONSerializer();
try {
jsonString = jsonSerializer.toJSON(inputStream);
} catch (MarshallException e) {
e.printStackTrace();
}
out.print(jsonString);
I get the below mentioned exception:
com.metaparadigm.jsonrpc.MarshallException: can't marshall sun.net.www.protocol.http.HttpURLConnection$HttpInputStream
at com.metaparadigm.jsonrpc.JSONSerializer.marshall(JSONSerializer.java:251)
at com.metaparadigm.jsonrpc.JSONSerializer.toJSON(JSONSerializer.java:259)
at Communicator.doGet(Communicator.java:33)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:740)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:247)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:193)
at filters.ExampleFilter.doFilter(ExampleFilter.java:149)
Make use of Jackson JSON parser.
Refer - Jackson Home
The only thing you need to do -
ObjectMapper mapper = new ObjectMapper();
Map<String, Object> jsonMap = mapper.readValue(inputStream, Map.class);
Now jsonMap will contain the JSON.
ObjectMapper.readTree(InputStream) easily let's you get nested JSON with JsonNodes.
public void testMakeCall() throws IOException {
URL url = new URL("https://api.coindesk.com/v1/bpi/historical/close.json?start=2010-07-17&end=2018-07-03");
HttpURLConnection httpcon = (HttpURLConnection) url.openConnection();
httpcon.addRequestProperty("User-Agent", "Mozilla/4.0");
InputStream is = httpcon.getInputStream();
try {
ObjectMapper mapper = new ObjectMapper();
JsonNode jsonMap = mapper.readTree(is);
JsonNode bpi = jsonMap.get("bpi");
JsonNode day1 = bpi.get("2010-07-18");
System.out.println(bpi.toString());
System.out.println(day1.toString());
} finally {
is.close();
}
}
Result:
{"2010-07-18":0.0858,"2010-07-19":0.0808,...}
0.0858
Better to save memory by having output as Stream<JsonNode>
private fun InputStream.toJsonNodeStream(): Stream<JsonNode> {
return StreamSupport.stream(
Spliterators.spliteratorUnknownSize(this.toJsonNodeIterator(), Spliterator.ORDERED),
false
)
}
private fun InputStream.toJsonNodeIterator(): Iterator<JsonNode> {
val jsonParser = objectMapper.factory.createParser(this)
return object: Iterator<JsonNode> {
override fun hasNext(): Boolean {
var token = jsonParser.nextToken()
while (token != null) {
if (token == JsonToken.START_OBJECT) {
return true
}
token = jsonParser.nextToken()
}
return false
}
override fun next(): JsonNode {
return jsonParser.readValueAsTree()
}
}
}