Reputation: 427
I am pretty new to Java and have an issue that I don't know how to fix. I read a few tutorials, posted questions etc. but I was still not able to understand how to Transfer the knowledge Fixing my Problem.
Here to Problem.
I want to read multiple json files all at one place and convert the data to Strings. I am able to read one data entry in the json, but not more. :-|
The file data Looks as follows:
{
"Header":{
"Liste1": {
"ID": "12345",
"Name" : "customerlist",
"Company List": [
"Company": "c1",
"Company": "c2",
"Company": "c3"
]
},
"Liste2":{
"ID": "12346",
"Name" : "vendorlist",
"Company List": [
"Company": "c4",
"Company": "c5",
"Company": "c6"
]
}
}
}
The code I used Main:
package testpaket;
import com.google.gson.Gson;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
public class test6 {
public static void main(String[] args) {
Gson gson = new Gson();
try (Reader reader = new FileReader("test.json")) {
// Convert JSON to Java Object
Header header = gson.fromJson(reader, Header.class);
System.out.println(header);
// Convert JSON to JsonElement, and later to String
/*JsonElement json = gson.fromJson(reader, JsonElement.class);
String jsonInString = gson.toJson(json);
System.out.println(jsonInString);*/
} catch (IOException e) {
e.printStackTrace();
}
}
}
My Lists class.
package testpaket;
import java.util.List;
public class Lists {
private String List;
private int ID;
private String Name;
private List<String> companyList;
}
My Header class.
package testpaket;
import java.util.Map;
public class Header {
String name;
String list;
int id;
String header1;
private Map<String, Lists> header;
//getters&setters
public String getHeader() {
return this.header1;
}
public void setHeader(String header) {
this.header1 = header1;
}
public String getList() {
return this.list;
}
public void setList(String list) {
this.list = list;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public int getID() {
return this.id;
}
public void setID(int id) {
this.id = id;
}
}
Does any of that even make sense? Sorry, but I really tried transfering the knowledge I have so far. Could anyone please help me and tell me how to fix it?
Upvotes: 12
Views: 66118
Reputation: 36
Here is the sample example of how you can read json file as a string. Suppose we have a json file located at "src/test/resources/sample.json"
public class ReadJsonAsString {
public static void main(String[] args) throws Exception {
String file = "src/test/resources/sample.json";
String json = readFileAsString(file);
System.out.println(json);
}
public static String readFileAsString(String file)throws Exception
{
return new String(Files.readAllBytes(Paths.get(file)));
}
}
Upvotes: 0
Reputation: 276
If you want to convert JSON-files to strings, it is not necessary to convert the file to a Java Object and after to a String. With apache commons-io library you can do this with only 1 line.
String exampleRequest = FileUtils.readFileToString(new File("exampleJsonRequest.json"), StandardCharsets.UTF_8);
Upvotes: 20
Reputation: 427
Here the complete solution using GSON:
package jsonProcessing;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
public class GsonHeader {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
BufferedReader reader = null;
try{
reader = new BufferedReader(new FileReader("C:/JSON/header.json"));
Gson gson = new GsonBuilder().create();
HeaderResult results = gson.fromJson(reader, HeaderResult.class);
if(results != null){
for(Header h : results.getHeader()){
String temp =("ID: "+h.getID()+" Name: "+h.getName());
for(CompanyList cl : h.getCompanyList()){
System.out.println(temp+" "+"Company: "+cl.getCompany());
}
}
}
}catch(Exception e){
e.printStackTrace();
}finally{
if(reader != null){
try{
reader.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
}
}
package jsonProcessing;
import java.util.List;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class HeaderResult {
@SerializedName("Header")
@Expose
private List<Header> header = null;
public List<Header> getHeader() {
return header;
}
public void setHeader(List<Header> header) {
this.header = header;
}
}
package jsonProcessing;
import java.util.List;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Header {
@SerializedName("ID")
@Expose
private String iD;
@SerializedName("Name")
@Expose
private String name;
@SerializedName("Company List")
@Expose
private List<CompanyList> companyList = null;
public String getID() {
return iD;
}
public void setID(String iD) {
this.iD = iD;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<CompanyList> getCompanyList() {
return companyList;
}
public void setCompanyList(List<CompanyList> companyList) {
this.companyList = companyList;
}
}
package jsonProcessing;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class CompanyList {
@SerializedName("Company")
@Expose
private String company;
public String getCompany() {
return company;
}
public void setCompany(String company) {
this.company = company;
}
}
Finally the output:
ID: 12345 Name: customerlist Company: c1
ID: 12346 Name: vendorlist Company: c6
Upvotes: 1
Reputation: 71
First of all the objects do not match the json. The Header object should contain two fields like Liste1 and Liste2 (probably of the same type). These on the other hand should contain id, name and companyList fields. Also, did you create the json? the Liste1 and Liste2 should probably be the part of collection in the json, inside the "[]" brackets, then instead of heaving two fields like Liste1 and Liste2 you could have Collection in Header object.
Upvotes: 1