JsonObject jar file - java

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.

Related

Java "Syntax Error on token ";", { expected after this token" error

I am stuck on an error I keep getting, I have isolated the area the error has developed but cant figure out the cause.
package guiProject;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.File;
import java.util.Scanner;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.io.IOException;
import java.nio.file.Paths;
import java.nio.file.Path;
import java.nio.file.Files;
import java.util.List;
import java.util.stream.Collectors;
import java.awt.EventQueue;
import javax.swing.JFrame;
import java.awt.CardLayout;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JTree;
public class mainWindow {
// imports and class definition are before this point
private Path adminList =
Paths.get("src/guiProject/AdminList.txt").toAbsolutePath();
try {
List<String> admins = Files.lines(adminList).collect(Collectors.toList());
} catch (IOException e) {
}
// rest of code
The error is occurring at the end of the line that defines adminList. Any help is appreciated.
Put your code inside a method - say pullAdminList . Moreover private access of adminList works just fine, because, again your methods - member of class, can access it:
public List<String> pullAdminList()
{
List<String> admins;
try
{
admins = Files.lines(adminList).collect(Collectors.toList());
}
catch (IOException e) {
}
return admins;
}

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

Difference between org.json.simple and org.json

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.

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.

Categories