0

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.

2
  • Have you done any research on parsing JSON using/with Java or on any of the 3rd party libraries like GSON, etc.? Commented 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! Commented Jun 16, 2017 at 18:11

2 Answers 2

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);
Sign up to request clarification or add additional context in comments.

6 Comments

do you have a practical example on how to solve this with the suggested solution, like an example line of code, thanks!
I would gladly, and I know you said "No more links", but this page is nothing but examples on using Google GSON. It's the same page I used when I used it for the first time and it really helped me format what I needed quickly! mkyong.com/java/…
I have also added a block of code to my answer. +1 if it helps!
Awesome, thank you! Do you know how I can reformat the imported json to the requested String/array? Thanks a lot for your help again!
Once you've reached the end of the code block that I posted, you should have the entire JSON as a string. From there, simple String manipulation functinos that you write can search the String for specific tags/words/formats. However, while I haven't used GSON in a while, I believe that there are methods in which you pull parts of the json as a string to minimize how much you have to manipulate
|
-1

There are JSON parson libraries that serve this exact purpose.

JSONArray

JSONString

Read more here.

1 Comment

do you have a practical exampl how to do it in my case?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.