How to parse a JSONString and turn its values into an Array?

321
June 22, 2017, at 1:43 PM

How to parse a JSONString, from a once JSONString.stringify simple array that now "appears flattened" inside, and turn its values back into a Java List or Java Array? (Using Jersey 1.x & Java) ? Array originally started as [1,2,3] before it was stringify-ed (reference https://www.w3schools.com/js/js_json_stringify.asp ).

Answer 1

I cannot understand exactly your original question, but one of the way is:

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import javax.swing.JFileChooser;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
public class JsonParser {
    JFileChooser chooser = new JFileChooser();
    File f;
    static String fn = "";
    static String js1 = "{\"name\": \"LALA\", \"email\": \"tst@tst.com\"}";
    String name = "name";
    String email = "email";
    String fName = "firsName";
    String city = "city";
    // ... other needed fields
    User u1 = null;
    public JsonParser() {
        parseFile();
        System.out.println("\n" + u1.toShortString());
    }
    private String openFchooser() throws FileNotFoundException, IOException, InterruptedException, Exception {
        int returnVal = chooser.showOpenDialog(null);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            f = chooser.getSelectedFile();
        }
        return f.getAbsolutePath();
    }
    // To parse JSON files with data
    //===========================================
    public void parseFile() {
        JSONParser parser = new JSONParser();
        try {
            // To parse obj 1
            Object obj1 = parser.parse(js1);
            System.out.println("User 1: " + obj1.toString());
            System.out.println();
            JSONObject jobj1 = (JSONObject) obj1;
            String from_name = jobj1.get(name).toString();
            String from_email = jobj1.get(email).toString();
//            String from_fName = jobj1.get(fName).toString();
//            String from_city = jobj1.get(city).toString();
            u1 = new User(from_name, from_email, null, null);
//            System.out.println(u1.toString() + "\n");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static void main(String[] args) {
        new JsonParser();
    }
    class User {
        String name = null;
        String email = null;
        String fName = null;
        String city = null;
        public User(String n, String e, String f, String c) {
            this.name = n;
            this.email = e;
            this.fName = f;
            this.city = c;
        }
        public String getFirsName() {
            return this.name;
        }
        public String setFirsName(String s) {
            return this.name = s;
        }
        public String getEmail() {
            return this.email;
        }
        public String setEmail(String s) {
            return this.email = s;
        }
        public String toString() {
            return "{\"name\":" + this.name + ", "
                    + "\"email\":" + this.email + ", "
                    + "\"firsName\":" + this.fName + ", "
                    + "\"city\":" + this.city + "\"}";
        }
        public String toShortString() {
            return "{\"name\": \"" + this.name + "\", "
                    + "\"email\": \"" + this.email + "\"}";
        }
    };
}

OUTPUT:

User 1: {"name":"LALA","email":"tst@tst.com"}

{"name": "LALA", "email": "tst@tst.com"}
READ ALSO
Java program not working while connected to VPN and works perfectly fine when not connected to VPN

Java program not working while connected to VPN and works perfectly fine when not connected to VPN

I have an issue with a Java program I'm creatingIt works perfectly well when I'm not connected to a VPN, but as soon as I connect, the program executes and stops at a certain point in the program

297
Use of a key to synchronize access to code block

Use of a key to synchronize access to code block

Normally I would lock on a critical section like the following

243
How to use relative file path for firefox profile in Selenium webdriver using java?

How to use relative file path for firefox profile in Selenium webdriver using java?

I want to use relative file path in Firefoxprofile function, I have tried all possible ways but its throwing errorHere is my code- profile

433
How to customize Navigation drawer with Image Button inside

How to customize Navigation drawer with Image Button inside

As picture shown below, I want to use navigation drawer to implement with the image button, if user swipe right, the navigation drawer is open and there is a only 1 image button show at middle

273