How to edit/write back into a json file? - java

below is my java code to read from a json file
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Iterator;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
public class FileIO {
#SuppressWarnings("unchecked")
//public static Showtime [] movie = MoviesListing.movie();
public void importTxt() {
int j = 0;
JSONParser parser = new JSONParser();
try {
JSONArray a = (JSONArray) parser.parse(new FileReader("C:/Users/Glambert/Dropbox/java/New folder/perfection/UPdate/json.txt"));
for (Object o : a)
{
JSONObject person = (JSONObject) o;
MoviesListing.movie[j].assign((String) person.get("moviename"));
MoviesListing.movie[j].assignShowingStatus((String) person.get("showingstatus"));
MoviesListing.movie[j].assignSynopsis((String) person.get("synopsis"));
MoviesListing.movie[j].assignDirector((String) person.get("director"));
MoviesListing.movie[j].assignCast((String) person.get("cast"));
MoviesListing.movie[j].assignReviewerRate((double) person.get("reviewerRate"));
MoviesListing.movie[j].assignPastReviews((String) person.get("pastreviews"));
MoviesListing.movie[j].assignMovieRating((String) person.get("movierating"));
MoviesListing.movie[j].assignShowtimes((JSONArray) person.get("showtimes"));
j++;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
}
}
It works from reading a json file like this:
[
{
"moviename":"Goosebumps",
"showingstatus":"Now Showing",
"synopsis":"Upset...",
"director":"Rob Letterman",
"cast":"Jack Black, Dylan Minnette, Odeya Rush, Amy Ryan",
"reviewerRate": 3.2 ,
"pastreviews":"Goosebumps...",
"movierating":"PG",
"showtimes":[
"09.30",
"12.30"
],
"job":"Teacher"
},
{
"moviename":"Bridge of Spies",
"showingstatus":"Now Showing",
"synopsis":"Tom Hanks stars..",
"director":"Steven Spielberg",
"cast":"Tom Hanks, Mark Rylance",
"reviewerRate": 3.9,
"pastreviews":"JBridge...",
"movierating":"PG-13",
"showtimes":[
"09.40",
"10.45"
],
}
]
However, I want to write/edit into the txt file in the same, or at least close to this format. Anyone knows how to do that?

In the JSON format you have provided, remove the comma after last array:
....
"showtimes":[
"09.40",
"10.45"
]
}
]
Solution:
You can use (Download the Jar file from Maven)
http://mvnrepository.com/artifact/com.google.code.gson/gson
And Create a Bean for Your Data as below:
MovieBean.java
private String moviename;
private String showingstatus;
...
private ArrayList<Double> showtimes;
private String job;
And After that you can use,
Gson methods fromJson and toJson for creating JSON to Bean and Bean to JSON respectively.
You can edit the data in Bean and save it again on the same content file.

Related

Read JSON data in Java using the org/json/json/20171018 repository

I'm trying to read a JSON file in my java application using the org/json/json/20171018 repository (http://central.maven.org/maven2/org/json/json/20171018/ -> json-20171018.jar). My JSON file looks like this:
{
"manifest_version": 2,
"name": "Chrome Extension",
"version": "0.1",
"permissions": [
"tabs"
],
"content_scripts": [
{
"matches": [
"<all_urls>"
],
"js": ["content.js"]
}
],
"background": {
"matches": [
"google.de",
"youtube.com",
"wikipedia.de"
],
"scripts": ["background.js"],
"persistent": true
}
}
I'm interested in the background section more specific in the links the background matches to. So I've created first a JSONObject of the whole file, then a JSONObject of the background section and then a JSONArray of the type matches. But unfortunately I'm getting this error showing up when I run the program:
Exception in thread "main" org.json.JSONException: JSONObject["matches"] not found.
at org.json.JSONObject.get(JSONObject.java:520)
at org.json.JSONObject.getJSONArray(JSONObject.java:714)
at Json.main(Json.java:19)
My java code looks like this:
import org.json.JSONArray;
import org.json.JSONObject;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
public class Json {
public static void main(String[] args){
String loc = new String("chromeAdon/manifest.json");
File file = new File(loc);
try {
String content = new String(Files.readAllBytes(Paths.get(file.toURI())));
JSONObject json = new JSONObject(content);
JSONObject json2 = new JSONObject(json.getJSONObject("background"));
JSONArray jarray = json2.getJSONArray("matches");
for (int i=0;i<jarray.length();i++){
System.out.println(jarray.getString(0));
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
Does anyone know where my mistake is?
You are wrapping the JSON object returned by getJSONObject("background"), which is not needed.
Try just using the returned object:
JSONObject jsonContent = new JSONObject(content);
JSONObject jsonBackground = jsonContent.getJSONObject("background");
JSONArray jsonArrayMatches = jsonBackground.getJSONArray("matches");

How to get values inside from an array which is inside of an object?

I want to get the values of latitude and longitude from the JSON, which consists of two objects "stoppage" & "routePlaceback", now I'm able to get data from "routePlaceback" only, but I have no clue how to get only the values of latitude and longitude? code is as follows,
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Iterator;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
public class Finder_Json
{
#SuppressWarnings("rawtypes")
public static void main(String[] args) throws Exception
{
// parsing JSON file
Object sampleFile_object = new JSONParser().parse(new FileReader("sample.json"));
// typecasting object to JSONObject
JSONObject sampleFile_JSONObject = (JSONObject) sampleFile_object;
JSONArray routePlaceback = (JSONArray) sampleFile_JSONObject.get("routePlaceback");
Iterator iterator_1 = routePlaceback.iterator();
while (iterator_1.hasNext())
{
System.out.println(iterator_1.next());
System.out.println("\n");
}
}
}
My sample.json file consists,
{
"stoppage":
[
{
"latitude": "23.074207",
"longitude": "72.557227",
"record_date": 1556217000,
"start_time": 1556217000,
"end_time": 1556304360,
"duration_time": 1456
}
],
"routePlaceback":
[
{
"distance": 0.36,
"longitude": "72.502385",
"ignition": 1,
"record_date": 1556303400,
"speed": 53.708,
"latitude": "23.034403"
},
{
"distance": 0.38,
"longitude": "72.506072",
"ignition": 1,
"record_date": 1556303430,
"speed": 25.927999,
"latitude": "23.034045"
}
]
}
This is what I get when I run the above code,
But my desired output is as,
23.034403, 72.502385
23.034045, 72.506072
You can extract the required values while displaying:
while (iterator_1.hasNext())
{
JSONObject obj = (JSONObject) iterator_1.next()l
System.out.println(obj.get("latitude")+" , "+obj.get("longitude"));
System.out.println("\n");
}
You are trying to access the property of an Object where you can get the Object.
while (iterator_1.hasNext())
{
JSONObject k= (JSONObject)iterator_1.next()
System.out.println(k.latitude+" "+k.longitude);
System.out.println("\n");
}
I modified a little bit your code to achieve your target. Please find the example below:
import java.io.FileReader;
import java.util.Iterator;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
public class Finder_Json
{
#SuppressWarnings("rawtypes")
public static void main(String[] args) throws Exception
{
// parsing JSON file
Object sampleFile_object = new JSONParser().parse(new FileReader("src/main/resources/sample.json"));
// typecasting object to JSONObject
JSONObject sampleFile_JSONObject = (JSONObject) sampleFile_object;
JSONArray routePlaceback = (JSONArray) sampleFile_JSONObject.get("routePlaceback");
Iterator iterator = routePlaceback.iterator();
while (iterator.hasNext()) {
JSONObject objt = (JSONObject) iterator.next();
System.out.println(objt.get("latitude") + ", " + objt.get("longitude"));
}
}
}
I strongly suggest to use Other libraries like GSON or Jackson anyway.

Read a Json File. Add an additional field depending on one of the field values. Write back Json

I have a json file that looks like
[{"field1":Value1,
"field2":value2,
"field3_find":[value3,value4],
"field4_find":{"sub1":subval1,"sub2":subvalue2}
}
{"field1":Value5,
"field2":value6,
"field3_find":[value7,value8],
"field4_find":{"sub1":subval3,"sub2":subvalue3}
}
]
I want to read the Json. Find a particular key value pair. Once a matching field is found , create a new field depending on the value of this field. Write back Json.
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.io.FileReader;
JSONParser parser = new JSONParser();
ArrayList<LinkedHashMap< String, JSONObject>> AllRows;
AllRows = (JSONArray) parser.parse(new FileReader(InputFile));
JSONArray New_rows =new JSONArray();
LinkedList<String> FieldNameAnotation=new LinkedList<String>();
FieldNameAnotation.add(new String("field3_find"));
FieldNameAnotation.add(new String("field4_find"));
try {
for(LinkedHashMap<String,JSONObject> map : AllRows){
JSONObject extend_row=new JSONObject();
for (Map.Entry<String, JSONObject> entry : map.entrySet()) {
if ((FieldNameAnotation).contains(entry.getKey()))
{
newvalue1=process1(entry.getKey());
newvalue2=process2(entry.getKey());// call a function process to get this value
extend_row.put(newvalue1,newvalue2);
}
}
extend_row.putAll(map);
New_rows.add(extend_row);
}
}catch (Exception e) {
System.err.println(e);
System.exit(0);
}
However this gives an error
org.json.simple.JSONObject cannot be cast to java.util.LinkedHashMap.
I know that it is because the way I have read the JSON .It is read as an array of JSON objects, But I do not how to read it as
ArrayList<LinkedHashMap< String, JSONObject>>

Android JSON names order

I'm using the following code to parse info from a site and it works expect that the older in the last for loop goes out of whack. names() comes out as
["569","570","565","566","567","568","562","563","564"]
those number should be in numeric order but they aren't. Is there a good way to fix this?
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;
import android.annotation.SuppressLint;
import android.util.Log;
public class JSON {
private String html;
private String version = "version";
private String pageString = null;
private String urlString = "http://frc-manual.usfirst.org/a/GetAllItems/ManualID=3";
public volatile boolean parsingComplete = true;
public JSON(String page){
this.pageString = page;
}
public String getHTML(){
return html;
}
public String getVersion(){
return version;
}
#SuppressLint("NewApi")
public void readAndParseJSON(String in) {
try {
JSONObject reader = new JSONObject(in);
JSONObject head = reader.getJSONObject("data").getJSONObject("SubChapter").getJSONObject("3").getJSONObject("children").getJSONObject(pageString);
html = head.getString("item_content_text");
if(head.has("children")){
JSONObject children = head.getJSONObject("children");
JSONArray sub1 = new JSONArray(children.names().toString());
for(int i=sub1.length()-1;i>=0;i--){
JSONObject children2 = children.getJSONObject(Integer.toString(sub1.getInt(i)));
html = html + "<h2>" + children2.getString("secdisp")+ " " + children2.getString("item_name") + "</h2>";
html = html + children2.getString("item_content_text");
if(children2.has("children")){
JSONObject children3 = children2.getJSONObject("children");
JSONArray sub2 = new JSONArray(children3.names().toString());
html = html + sub2;
for(int j=sub2.length()-1;j>=0;j--){
JSONObject children4 = children3.getJSONObject((String) sub2.get(j));
html = html + "<h3>" + children4.getString("secdisp")+ " " + children4.getString("item_name") + "</h3>";
html = html + children4.getString("item_content_text");
}
}
}
}
JSONObject main = reader.getJSONObject("data");
version = main.getString("LatestManualUpdate");
parsingComplete = false;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void fetchJSON(){
Thread thread = new Thread(new Runnable(){
#Override
public void run() {
try {
URL url = new URL(urlString);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(10000 /* milliseconds */);
conn.setConnectTimeout(15000 /* milliseconds */);
conn.setRequestMethod("GET");
conn.setDoInput(true);
// Starts the query
conn.connect();
InputStream stream = conn.getInputStream();
String data = convertStreamToString(stream);
readAndParseJSON(data);
stream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
});
thread.start();
}
static String convertStreamToString(java.io.InputStream is) {
java.util.Scanner s = new java.util.Scanner(is).useDelimiter("\\A");
return s.hasNext() ? s.next() : "";
}
}
The order of the keys in a JSONObject is undefined (see the documentation for keys()). In practice, this means they will likely be ordered according to their hash codes, which is clearly not what you want. If you need to keep these items in a specific order, you could either:
Use a different JSON parser. There are plenty available with a google search; I'd suggest that perhaps a "push-parser" may be the most appropriate type, as this will be guaranteed to give you the items in the object one after another in the order they are defined. json-simple is one such parser. Look at their examples 5 & 6 in the "decoding examples" page. Alternatively, see example 4 for how to change the type of Map it uses for storing JSON objects in its object-model mode, and note that a LinkedHashMap preserves the order the values are added to it.
Sort the list of names before you use them
Change the JSON to use an array of objects which contain the number rather than an object with the numbers as keys, as arrays are (obviously) kept in the order they appear in the original JSON.
You are expecting something that is outside the representational model for JSON.
According to the JSON specification:
"An object is an unordered set of name/value pairs."
The fact that you can imply an order is not relevant. The JSON information model does not recognize that such an ordering exists. That is why most JSON parsers ignore your implied ordering, and many in-memory JSON object representations have no way of recording it.
So what should you do?
My recommendation would be to change your JSON schema. For example, instead of this:
{ "name1" : "value1", "name2" : "value2", "name3" : "value3" }
write:
[ [ "name1", "value1" ], [ "name2", "value2" ], [ "name3", "value3" ] ]
or
[ { "name": "name1", "val" : "value1" },
{ "name": "name2", "val" : "value2" },
{ "name": "name3", "val" : "value3" } ]
... both of which are guaranteed to preserve the order of the entries.
It is also possible to "hack" a solution; i.e. there are ways to get some JSON parsers to preserve the order of name/value pairs that is implied by the serial form. (I'm not going to explain how, because I think it is a really bad idea.)
The problem with doing this is that it ignores interoperability. One of the most important reasons for using JSON (or XML, or any other standard format) is that people can use any spec-conformant software to generate and parse the "stuff" used for data interchange. But what you are doing is taking away that advantage by adding extra requirements that directly contradict the JSON spec.

Convert xml to json in java

I want to convert from xml to json in java and get an output.
But whike doing so only some parts from the xml are getting converted and not the whole xml.
Any help
My input xml is :
<?xml version="1.0" encoding="UTF-8"?>
<important-data certified="true" processed="true">
<timestamp>232423423423</timestamp>
<authors>
<author>
<firstName>Tim</firstName>
<lastName>Leary</lastName>
</author>
</authors>
<title>Flashbacks</title>
<shippingWeight>1.4 pounds</shippingWeight>
<isbn>978-0874778700</isbn>
</important-data>
And my code is:
package com.discursive.answers;
import java.io.IOException;
import java.io.InputStream;
import org.apache.commons.io.IOUtils;
import net.sf.json.JSON;
import net.sf.json.JSONException;
import net.sf.json.JSONObject;
import net.sf.json.xml.XMLSerializer;
import org.json.XML;
public class ConvertXMLtoJSON {
public static int PRETTY_PRINT_INDENT_FACTOR = 4;
public static String TEST_XML_STRING = "<?xml version=\"1.0\" ?>" +
"<test attrib=\"moretest\">" +
"Turn this to JSON</test>";
public static void main(String[] args) {
new ConvertXMLtoJSON();
}
public ConvertXMLtoJSON() {
try {
InputStream is = ConvertXMLtoJSON.class.getResourceAsStream("sample-xml.xml");
if (is != null) {
String xml = IOUtils.toString(is);
JSON json = XMLSerializer.readObject(xml);
System.out.println(json.toString().split(",").length);
for(int i= 0 ;i < json.toString().split(",").length; i ++)
System.out.println(json.toString().split(",")[i]);
} else {
System.out.println("Checkpoint 1");
}
} catch (java.io.IOException ioe) {
ioe.printStackTrace();
}
}
Applying the above mentioned xml and code I get the output as:
{"timestamp":"232423423423"
"authors":"\n\t\t\n\t\t\tTim\n\t\t\tLeary\n\t\t\n\t"
"title":"Flashbacks"
"isbn":"978-0874778700"
"shippingWeight":"1.4 pounds"}
where as it should be as :
{
"#certified": "true",
"#processed": "true",
"timestamp": "232423423423",
"authors": [ {
"firstName": "Tim",
"lastName": "Leary"
}],
"title":
"Flashbacks",
"shippingWeight": "1.4 pounds",
"isbn": "978-0874778700"
}
What changes should I do to get the required output?
The article How to Convert XML to JSON in Java gives the same example as yours using json-lib.
The code in the example works, so it might be worth checking the versions on your libs. The json-lib website lists the required libs.
I managed to get mine working with the following libs on my build path:
commons-beanutils-1.8.3
commons-collections-3.2.1
commons-io-1.2
commons-lang-2.4
commons-logging-1.1.3
ezmorph-1.0.6
json-lib2.4-jdk15
xom-1.2.10

Categories