Difference between org.json.simple and org.json - java

JSONArray js1 = new JSONArray();
for (Product product : plist) {
JSONObject jo1 = new JSONObject();
jo1.put("image", product.getProductImages());
jo1.put("name", product.getName());
jo1.put("price", product.getPrice());
js1.add(jo1);
}
In the above code I can not use js1.add(jo1); because of I import these Libraries.
import org.json.JSONArray;
import org.json.JSONObject;
but if I import these
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
I can use add method.
So what is the difference between these org.json.simple. and org.json.

You have two different dependencies on your classpath.
import org.json.JSONArray;
import org.json.JSONObject;
Above two imports come from JSON-java
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
Above two imports come from json-simple
These are two different implementation of JSON processor. So, it's obvious that the contract of these two dependencies don't match. There are other JSON processor listed on json.org.

Related

Where is "from" imported from camel in java?

I am trying to import csv file to xml file
i see apache has feature to do
from(in)
.to(out)
.split(body().tokenize("\n")).streaming()
.unmarshal().csv();
but i have a "cannot resolve method 'from(java.lang.String)' error
when I try to import then i cannot find any packages for camel
this one works:
import org.apache.camel.dataformat.bindy.csv.BindyCsvDataFormat;
what is the package to use "from" from org.apache.camel.???
this is my file:
import org.apache.camel.Exchange;
import org.apache.camel.dataformat.bindy.BindyAbstractDataFormat;
import org.apache.camel.dataformat.bindy.BindyAbstractFactory;
import org.apache.camel.dataformat.bindy.BindyFixedLengthFactory;
import org.apache.camel.dataformat.bindy.FormatFactory;
import org.apache.camel.dataformat.bindy.csv.BindyCsvDataFormat;
import org.apache.camel.dataformat.bindy.util.ConverterUtils;
import org.apache.camel.spi.DataFormat;
import org.apache.camel.util.IOHelper;
import org.apache.camel.util.ObjectHelper;
import org.apache.camel.CamelContext;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.Exchange;
import org.apache.camel.Message;
import org.apache.camel.Processor;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.impl.DefaultCamelContext;
public class Csvtoxml {
public static void convert(String in, String out) throws Exception {
DataFormat bindy = new BindyCsvDataFormat(Model.class);
from("myCsvFile.csv")
.to("myXmlFile.xml")
.split(body().tokenize("\n")).streaming()
.unmarshal().csv();
}
}
It is not imported from anywhere. In order to use it this way you have to inherit your class from org.apache.camel.builder.RouteBuilder

JsonObject jar file

I imported the following imports to my code, however I still get an error for
JsonObject obj = new JsonParser().parse(input).getAsJsonObject();
"JsonObject cannot be resolved for a type."
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import org.json.JSONArray.*;
import org.json.JSONException;
import org.json.JSONObject.*;
import org.json.JSONString;
Its JSONObject not JsonObject.
Same goes for the parser, it's JSONParser not JsonParser.
JSON is always uppercase.

Benders.Strategy using Java and opl

I'm solving a mathematical model using Java however when i tried to call the Benders Strategy i keep receiving this error:
Exception in thread "main" java.lang.IllegalArgumentException: No enum class ilog.cplex.cppimpl.IloCplex$IntParam with value 1501
at ilog.cplex.cppimpl.IloCplex$IntParam.swigToEnum(IloCplex.java:1974)
at ilog.opl.IloCplex.setParam(IloCplex.java:5640)
Here's a part of my code in Java (i'm using CPLEX 12.8 and the library oplall.jar) :
import ilog.concert.IloException;
import ilog.concert.IloIntMap;
import ilog.concert.IloIntSet;
import ilog.concert.IloSymbolSet;
import ilog.opl.IloCplex;
import ilog.opl.IloOplDataSource;
import ilog.opl.IloOplErrorHandler;
import ilog.opl.IloOplFactory;
import ilog.opl.IloOplModel;
import ilog.opl.IloOplModelDefinition;
import ilog.opl.IloOplModelSource;
import ilog.opl.IloOplSettings;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.PrintWriter;
...
IloOplFactory.setDebugMode(false);
IloOplFactory oplF = new IloOplFactory();
IloOplModelSource source = oplF.createOplModelSource("Model.mod");
IloOplDataSource dataSource = oplF.createOplDataSource("Instance.dat");
IloOplErrorHandler handler = oplF.createOplErrorHandler();
IloOplSettings settings = oplF.createOplSettings(handler);
IloOplModelDefinition def = oplF.createOplModelDefinition(source, settings);
IloCplex cplex = new IloCplex();
IloOplModel opl = oplF.createOplModel(def, cplex);
opl.addDataSource(dataSource);
cplex.setParam(IloCplex.IntParam.Benders.Strategy, 3);
opl.generate();
cplex.solve();
cplex.end();
There's a similar question here.
In model.mod you could write:
execute { cplex.bendersstrategy=3; }

.keys() method not working on JSONObject eclipse

i am trying to use the .keys() method to get the name of JSON Object's
the code im using is;
Iterator<String> keys = JSONObject.keys();
.keys() is underelined as red on eclipse, i dont know why, can any one help, thanks! -
I have JSON simple as an external library and have imported it, not sure what else to do
EDIT:
Here is more code;
JSONParser parser = new JSONParser();
FileReader testfile = new FileReader("test2.txt");
Object obj = parser.parse(testfile);
JSONObject jsonObject = (JSONObject) obj;
JSONObject name = (JSONObject) jsonObject.get("txt");
String time = (String) name.get("name");
JSONObject example2 = (JSONObject) jsonObject.get("birth");
System.out.println(example2);
Iterator keys = example2 .keys(); <-- where the red line shows up
Second edit:
here are my imports.
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.Set;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
try:
Iterator<String> keys = example2.keySet().iterator();
With this artifact: https://mvnrepository.com/artifact/org.json/json/20170516
Use the following code:
JSONObject obj = new JSONObject("{\"key\":\"value\"}");
for(Object o : obj.keys()) { ... }
It will work.
Do not mix API.

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>>

Categories