I am reading a JSON file into one string and one array. I already have a string where the JSON is saved, let's call it myString. Here is the JSON file:
JSON File
As you can see, the file contains three styles, from "styleCount": "3". My goal is to now create three string variables for each style, similar to the following pseudo variables:
String name_style1 should contain: "Sommer-Fashion"
String name_style2 should contain: "Dream-Style"
String name_style3 should contain: "Perfect-Look"
Then I need an array of strings for each style with the SKU numbers:
private String[] sku_style1 = new String[6];
sku_style1[0] = "392714";
sku_style1[1] = "395895";
sku_style1[2] = "392450";
sku_style1[3] = "371706";
sku_style1[4] = "383748";
sku_style1[5] = "385275";
And also for the other styles:
private String[] sku_style2 = new String[6];
private String[] sku_style3 = new String[6];
Is there a function of Java which helps with simply adding elements from a JSON file (or in my case a string: myString) into a string and an array?
Any help is appreciated.
-
Have you done any research on parsing JSON using/with Java or on any of the 3rd party libraries like GSON, etc.?Jonny Henly– Jonny Henly2017年06月16日 17:38:09 +00:00Commented Jun 16, 2017 at 17:38
-
hey guys, code examples would be appreciated...just sending me links doesn't help me too much, since I am not that experienced. Thanks a lot!Manuel– Manuel2017年06月16日 18:11:28 +00:00Commented Jun 16, 2017 at 18:11
2 Answers 2
Google GSON! No functions native to Java really help much, but Google GSON has helped me numerous times with issues much harder than this. I think you'll find it very helpful. Here is a link!
https://mvnrepository.com/artifact/com.google.code.gson/gson
EDIT
This link is for the repository for the jar downloads!
EDIT 2
Gson gson = new Gson();
Staff obj = new Staff();
// 1. Java object to JSON, and save into a file
gson.toJson(obj, new FileWriter("D:\\file.json"));
// 2. Java object to JSON, and assign to a String
String jsonInString = gson.toJson(obj);