I have json'ed array of host, port, uri tuples encoded by 3rd party as an array of fixed length arrays:
[
["www1.example.com", "443", "/api/v1"],
["proxy.example.com", "8089", "/api/v4"]
]
I would like to use jackson magic to get a list of instances of
class Endpoint {
String host;
int port;
String uri;
}
Please help me to put proper annotations to make ObjectMapper to do the magic.
I do not have control on the incoming format and all my google'n ends in answers on how to map array of proper json objects (not arrays) into list of objects (like https://stackoverflow.com/a/6349488/707608)
=== working solution as advised by https://stackoverflow.com/users/59501/staxman
in https://stackoverflow.com/a/38111311/707608
public static void main(String[] args) throws IOException {
String input = "" +
"[\n" +
" [\"www1.example.com\", \"443\", \"/api/v1\"],\n" +
" [\"proxy.example.com\", \"8089\", \"/api/v4\"]\n" +
"]";
ObjectMapper om = new ObjectMapper();
List<Endpoint> endpoints = om.readValue(input,
new TypeReference<List<Endpoint>>() {});
System.out.println("endpoints = " + endpoints);
}
#JsonFormat(shape = JsonFormat.Shape.ARRAY)
static class Endpoint {
#JsonProperty() String host;
#JsonProperty() int port;
#JsonProperty() String uri;
#Override
public String toString() {
return "Endpoint{host='" + host + '\'' + ", port='" + port + '\'' + ", uri='" + uri + '\'' + '}';
}
}
Add following annotation:
#JsonFormat(shape=JsonFormat.Shape.ARRAY)
class Endpoint {
}
and it should serialize entries as you wish.
Also: it'd be safest to then use #JsonPropertyOrder({ .... } ) to enforce specific ordering, as JVM may or may not expose fields or methods in any specific order.
Consider importing the two dimensional array and then running through a loop. Then you can have a constructor on the Endpoint class that accepts each instance of internal array.
Related
I've been trying to save a list of data as Strings, but when I try a method .toString on List, it returns address in memory instead of data.
public class Item {
Integer price = 20;
Integer modelNumber = 100;
String description = "Description";
String title = "Title";
Boolean wasBought = true;
}
public static void main(String[] args) {
List<Item> data = new ArrayList<>();
data.add(new Item());
System.out.println(data.toString());
}
You need to override toString function in your Item class. Add the following snippet into your Item class:
#Override
public String toString() {
return "Item{" +
"price=" + price +
", modelNumber=" + modelNumber +
", description='" + description + '\'' +
", title='" + title + '\'' +
", wasBought=" + wasBought +
'}';
}
Output:
[Item{price=20, modelNumber=100, description='Description', title='Title', wasBought=true}]
You can convert List to json format string by json utils, e.g. jackson or fastjson, in case you may need to convert it to Objects later.
Simply use Lombok (Add Lombok jar into classpath) #ToString annotate for Item class, it will do the needful output https://projectlombok.org/features/ToString
I'm a complete beginner to Java and I have been given an exercise where I have to read data from a CSV file and then create an object for each line of the file as the program reads the data from the file.
Here is part of the CSV file:
1,Jay, Walker,91 Boland Drive,BAGOTVILLE,NSW,2477
2,Mel, Lowe,45 Ocean Drive,MILLERS POINT,NSW,2000
3,Hugh, Manatee,32 Edgecliff Road,REDFERN,NSW,2016
4,Elizabeth, Turner,93 Webb Road,MOUNT HUTTON,NSW,2290
and so on ...
Here is my code that reads data from the CSV file:
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Client_19918424 {
public static void main(String[] args) throws FileNotFoundException {
File inFile = new File("clients.txt");
Scanner inputFile = new Scanner(inFile);
String str;
String[] tokens;
while (inputFile.hasNext()) {
str = inputFile.nextLine(); // read a line of text from the file
tokens = str.split(","); // split the line using commas as delimiter
System.out.println("Client ID: " + tokens[0]);
System.out.println("Client First Name: " + tokens[1]);
System.out.println("Client Sur Name: " + tokens[2]);
System.out.println("Street Address: " + tokens[3]);
System.out.println("Suburb: " + tokens[4]);
System.out.println("State: " + tokens[5]);
System.out.println("Postcode:" + tokens[6]);
System.out.println( );
} // end while
}
}
this is my Client class (have constructor):
public class Client {
private int clientID;
private String firstName;
private String surName;
private String street;
private String suburb;
private String state;
private int postcode;
// constructor
public Client (int ID, String fName, String sName, String str, String sb, String sta, int pCode) {
clientID = ID;
firstName = fName;
surName = sName;
street = str;
suburb = sb;
state = sta;
postcode = pCode;
}
However I don't know how to create a Client object for each line of text file as the program reads data from file.
like for the first line make something like this:
Client client1 = new Client(1, "Jay", "Walker", "91 Boland Drive", "BAGOTVILLE", "NSW", 2477);
And then add it to array:
Client[0] = client1;
can someone help me to solve this question, im really appreciate.
You are almost there.
All that's left to do is to map each token that is already printed to the corresponding fields in the Client class. Since token[0] doesn't really tell what value it holds you could do it in three ways:
while (inputFile.hasNext()) {
str = inputFile.nextLine();
tokens = str.split(",");
// Because tokens[0] is of type String but clientID is of type int,
// we need to parse it and get the integer representation.
int clientID = Integer.parseInt(tokens[0]);
// Both of type String, no parsing required.
String firstName = tokens[1];
String surName = tokens[2];
String street = tokens[3];
String suburb = tokens[4];
String state = tokens[5];
int postcode = Integer.parseInt(tokens[6]);
// Then all that's left to do is to create a new object of `Client` type
// and pass all the gathered information.
Client client = new Client(clientID, firstName, surName, street, suburb, state, postcode);
System.out.println(client + "\n");
}
At this moment if we try to print the client (last line) we will get something like this: com.example.demo.Client#30a3107a. That's because we didn't tell how we want our object to be displayed. For that toString() method in Client class has to be overriden like so:
#Override
public String toString() {
return "Client ID: " + clientID + "\n" + "Client First Name: " + firstName + "\n"
+ "Client Sur Name: " + surName + "\n" + "Street Address: " + street + "\n"
+ "Suburb: " + suburb + "\n" + "State: " + state + "\n" + "Postcode: " + postcode;
}
It will give the exact output that is in your example.
It is achievable to create the class by passing those tokens directly as well, without the creation of temporary variables:
Client client = new Client(Integer.parseInt(tokens[0]), tokens[1], tokens[2], tokens[3], tokens[4], tokens[5], Integer.parseInt(tokens[6]));
This case brings us to the third solution with setters and getters.
The variables that describe the Client are already defined, it is possible to pass them to assemble the perfect object, but it is not possible to retrieve them. Instead of setting the variables directly in the constructor, we can create a special method that will do the job, for instance:
// Other fields omitted
private int clientID;
// The empty constructor required for later usage,
// since right now, we can't create the object without specifying every property.
public Client() {
}
// This method does exactly the same thing that was done before but
// in the constructor directly
public void setClientID(int clientID) {
this.clientID = clientID;
}
// This method will assist in retrieving the set data from above.
public int getClientID() {
return clientID;
}
And then the while loop would look like this instead:
Client client = new Client();
client.setClientID(Integer.parseInt(tokens[0]));
client.setFirstName(tokens[1]);
client.setSurName(tokens[2]);
client.setStreet(tokens[3]);
client.setSuburb(tokens[4]);
client.setState(tokens[5]);
client.setPostcode(Integer.parseInt(tokens[6]));
And to get those values:
System.out.println("Client ID: " + client.getClientID());
Or you could use the constructor with the fields to create the client, add getters in the class, omit both setters, and the empty constructor if the creation of the client should only be possible with all the fields present.
I'm working on developping a webservice that communicate two application with each other, the first application will send a json object to the second one.
I'm stuck in translating this json object :
$body = "{"fields":{"project":{"key":"'+$projectKey+'"}
,"issuetype":{"name": "'+$issueType+'"}
,"summary":"'+$summary+'"
,"description":"'+$description+'"
,"customfield_12721":"'+$FirstName+'"
,"customfield_12722":"'+$LastName+'"
,"customfield_12723":{"value":"'+$EmployeeCategory+'"}
,"customfield_12732":"'+$Externalfunction+'"
,"customfield_12725":"'+$CorporateID+'"
,"customfield_12726":{"value":"'+$VermegCompany+'"}
,"customfield_12685":{"value":"'+$IndusRegion+'"}
,"customfield_12673":{"value":"'+$Product+'"}
,"customfield_12727":{"value":"'+$Profile+'"}
,"customfield_12667":{"name":"'+$Manager+'"}
,"customfield_12708":"'+$BeginDate+'"
,"customfield_14000":"'+$Reglementation+'"
,"customfield_14001":"'+$Department+'"
,"customfield_14002":"'+$SubDepartment+'"}
}";
To a String variable like :
String json = "{fields:{project:{\"key\":\""+ projectkey +"}"
+ "\",\"issuetype\":\""
+ "\",\"customfield_12721\":\"" + employee.getFirstName()
+ "\",\"description\":\"" + description
+ "\",\"summary\":\"" + summary
+ "\",\"customfield_12722\":\""+ employee.getLastName()
+ "\",\"customfield_12732\":\"" + employee.getFte()
+ "\",\"customfield_14000\":\"" + employee.getReglementation()
+ "\",\"customfield_14001\":\"" + employee.getDepartment()
+ "\",\"customfield_14002\":\"" + employee.getSubdepartment()
+ "\",\"fulltime\":" + Math.round(Double.parseDouble(employee.getFulltime().replaceAll(",",".")))
//+ ",\"email\":\"" + employee.getEmail()
+ ",\"citizenship\":\"" + employee.getCitizenship()
+ "\",\"gnn\":\""+ employee.getGnn()
+ "\",\"company\":\"" + employee.getCompany()
+ "\",\"employeeid\":\"" + employee.getEmployeeid()
+ "\",\"customfield_12708\":\"" + employee.getStartdate()
//+ "\",\"enddate\":\"" //+ employee.getEnddate()
+ "\",\"product\":\"" + employee.getProduct()
+ "\",\"customfield_12725\":\"" + employee.getInternalnumber()
// + "\",\"employeeid\":\"" + employee.getEmployeeid()
+ "\"}}";
Can you please help ?
Here you can use JsonConvert library to get json data.bellow are the example how to use it
String json = "{\"FirstName\":\"Jack\",\"LastName\":\"Tor\"}";
var data = Newtonsoft.Json.JsonConvert.DeserializeObject(json);
Console.WriteLine(data);
Console.ReadLine();
Using a web tool
If you just want to convert it fast into a string, you can use a Json to String converter on the web.
Using a library
If you however want a good solution in you applications, a way to solve this problem would be to serialize and parse using a library such as GSON. This is of course, if you don't intend to create this conversion yourself.
GSON is quite easy to use and takes care of the translation for you. Se example below:
The sending application:
Gson gson = new Gson();
String jsonStr = gson.toJson(employee); // Serialize (from Java class to JSON string)
// Send data (jsonStr) ...
The receiving application:
// Receive data (jsonStr) ...
Gson gson = new Gson();
Employee employee = gson.fromJson(jsonStr); // Parse (from JSON string to Java class)
GSON will by default name the fields the same as the member variable names in the Java class. If you need to change the field names in the JSON string, you can use #SerializedName("newName") in front of the member variables.
Example:
class Employee {
...
#SerializedName("customfield_12721") String firstName;
...
}
One of my webservice return below Java string:
[
{
id=5d93532e77490b00013d8862,
app=null,
manufacturer=pearsonEducation,
bookUid=bookIsbn,
model=2019,
firmware=[1.0],
bookName=devotional,
accountLinking=mandatory
}
]
I have the equivalent Java object for the above string. I would like to typecast or convert the above java string into Java Object.
I couldn't type-cast it since it's a String, not an object. So, I was trying to convert the Java string to JSON string then I can write that string into Java object but no luck getting invalid character "=" exception.
Can you change the web service to return JSON?
That's not possible. They are not changing their contracts. It would be super easy if they returned JSON.
The format your web-service returns has it's own name HOCON. (You can read more about it here)
You do not need your custom parser. Do not try to reinvent the wheel.
Use an existing one instead.
Add this maven dependency to your project:
<dependency>
<groupId>com.typesafe</groupId>
<artifactId>config</artifactId>
<version>1.3.0</version>
</dependency>
Then parse the response as follows:
Config config = ConfigFactory.parseString(text);
String id = config.getString("id");
Long model = config.getLong("model");
There is also an option to parse the whole string into a POJO:
MyResponsePojo response = ConfigBeanFactory.create(config, MyResponsePojo.class);
Unfortunately this parser does not allow null values. So you'll need to handle exceptions of type com.typesafe.config.ConfigException.Null.
Another option is to convert the HOCON string into JSON:
String hoconString = "...";
String jsonString = ConfigFactory.parseString(hoconString)
.root()
.render(ConfigRenderOptions.concise());
Then you can use any JSON-to-POJO mapper.
Well, this is definitely not the best answer to be given here, but it is possible, at least…
Manipulate the String in small steps like this in order to get a Map<String, String> which can be processed. See this example, it's very basic:
public static void main(String[] args) {
String data = "[\r\n"
+ " {\r\n"
+ " id=5d93532e77490b00013d8862, \r\n"
+ " app=null,\r\n"
+ " manufacturer=pearsonEducation, \r\n"
+ " bookUid=bookIsbn, \r\n"
+ " model=2019,\r\n"
+ " firmware=[1.0], \r\n"
+ " bookName=devotional, \r\n"
+ " accountLinking=mandatory\r\n"
+ " }\r\n"
+ "]";
// manipulate the String in order to have
String[] splitData = data
// no leading and trailing [ ] - cut the first and last char
.substring(1, data.length() - 1)
// no linebreaks
.replace("\n", "")
// no windows linebreaks
.replace("\r", "")
// no opening curly brackets
.replace("{", "")
// and no closing curly brackets.
.replace("}", "")
// Then split it by comma
.split(",");
// create a map to store the keys and values
Map<String, String> dataMap = new HashMap<>();
// iterate the key-value pairs connected with '='
for (String s : splitData) {
// split them by the equality symbol
String[] keyVal = s.trim().split("=");
// then take the key
String key = keyVal[0];
// and the value
String val = keyVal[1];
// and store them in the map ——> could be done directly, of course
dataMap.put(key, val);
}
// print the map content
dataMap.forEach((key, value) -> System.out.println(key + " ——> " + value));
}
Please note that I just copied your example String which may have caused the line breaks and I think it is not smart to just replace() all square brackets because the value firmware seems to include those as content.
In my opinion, we split the parse process in two step.
Format the output data to JSON.
Parse text by JSON utils.
In this demo code, i choose regex as format method, and fastjson as JSON tool. you can choose jackson or gson. Furthermore, I remove the [ ], you can put it back, then parse it into array.
import com.alibaba.fastjson.JSON;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class SerializedObject {
private String id;
private String app;
static Pattern compile = Pattern.compile("([a-zA-Z0-9.]+)");
public static void main(String[] args) {
String str =
" {\n" +
" id=5d93532e77490b00013d8862, \n" +
" app=null,\n" +
" manufacturer=pearsonEducation, \n" +
" bookUid=bookIsbn, \n" +
" model=2019,\n" +
" firmware=[1.0], \n" +
" bookName=devotional, \n" +
" accountLinking=mandatory\n" +
" }\n";
String s1 = str.replaceAll("=", ":");
StringBuffer sb = new StringBuffer();
Matcher matcher = compile.matcher(s1);
while (matcher.find()) {
matcher.appendReplacement(sb, "\"" + matcher.group(1) + "\"");
}
matcher.appendTail(sb);
System.out.println(sb.toString());
SerializedObject serializedObject = JSON.parseObject(sb.toString(), SerializedObject.class);
System.out.println(serializedObject);
}
}
I am attempting to parse the following JSON using Java:
{ "student_id": "123456789", "student_name": "Bart Simpson", "student_absences": 1}
What is the simplest way to accomplish this. I tried doing it the way below but think there must be an easier way.
import org.json.*
JSONObject obj = new JSONArray("report");
for(int i = 0; I < arr.length(); i++){
String studentname =
arr.getJSONObject(i).getString("student_id");
arr.getJSONObject(i).getString("student_name");
arr.getJSONObject(i).getString("student_name");
}
There's Gson:
import com.google.gson.Gson;
import com.google.gson.annotations.SerializedName;
public class Main {
public static void main(String[] args) {
String json = "{ \"student_id\": \"123456789\", \"student_name\": \"Bart Simpson\", \"student_absences\": 1}";
Student student = new Gson().fromJson(json, Student.class);
System.out.println(student);
}
}
class Student {
#SerializedName("student_id")
String studentId;
#SerializedName("student_name")
String studentName;
#SerializedName("student_absences")
Integer studentAbsences;
#Override
public String toString() {
return "Student{" +
"studentId='" + studentId + '\'' +
", studentName='" + studentName + '\'' +
", studentAbsences=" + studentAbsences +
'}';
}
}
Another popular one is Jackson:
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;
public class Main {
public static void main(String[] args) throws Exception {
String json = "{ \"student_id\": \"123456789\", \"student_name\": \"Bart Simpson\", \"student_absences\": 1}";
Student student = new ObjectMapper().readValue(json, Student.class);
System.out.println(student);
}
}
class Student {
#JsonProperty("student_id")
String studentId;
#JsonProperty("student_name")
String studentName;
#JsonProperty("student_absences")
Integer studentAbsences;
#Override
public String toString() {
return "Student{" +
"studentId='" + studentId + '\'' +
", studentName='" + studentName + '\'' +
", studentAbsences=" + studentAbsences +
'}';
}
}
In both cases, running Main will print:
Student{studentId='123456789', studentName='Bart Simpson', studentAbsences=1}
EDIT
And without creating a Student class, you could give something like JsonPath a try.
The answer to your question depends on what you want to achieve. Your example would result in an array of strings. The previous answer results in a Java class with a property for each field. Another option is to put the values into a map.
If have written an encoder / decoder combination for this. Encoding is quite easy: use the keys and values of the map. The decoder (JSON string to map or anything else) requires a parser (best would be a tokenizer).