0

I have the following json string I want to convert to java code so I can alter it easier and send the newer request but I cannot seem to figure out how to generate it with JSONObject and JSONArray.

["guest_login",{"chips":{"1":2000},"epoch":1552509677,"invested":{"1":2000},"login_id":10,"news":["Welcome to Poker.","Register and get started with 2000 play chips."],"username":"Guest10"}]

I get the following output

output = ["guest_login",null,null]

Here is what I tried so far.

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.JSONValue;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

    JSONObject obj = new JSONObject();
    JSONArray list = new JSONArray();

    list.add("guest_login");
    list.add(new JSONObject().put("chips", new JSONObject().put("1", "2000")));
    list.add(obj.put("epoch", "1552509677"));
    System.out.println("output = " + list.toString());

also tried

        JSONObject json = new JSONObject();
        json.put("name", "student");

        JSONArray array = new JSONArray();
        JSONObject item = new JSONObject();
        item.put("invested", new JSONObject().put("1", "2000"));
        item.put("login_id", "10");         
        item.put("epoch", "1552509677");
        item.put("chips", new JSONObject().put("1", "2000"));
        item.put("news", new JSONArray().add("Welcome to Poker.").add("Register and get started with 2000 play chips."));
        item.put("username","Guest10");
        json.put("guest_login", new JSONArray().add(item));

        message = json.toString();

        System.out.println("output = " + message);
1
  • You don't "convert JSON to Java code." You can read JSON into a Java object, but you need to define the object first. It is not clear what you're trying to do here. Commented Mar 13, 2019 at 23:47

2 Answers 2

1

I just run this code and works fine, just change different library:

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

...

        JSONObject obj = new JSONObject();
        JSONArray list = new JSONArray();

        list.put("guest_login");
        try {
            list.put(new JSONObject().put("chips", new JSONObject().put("1", "2000")));
            list.put(obj.put("epoch", "1552509677"));
        } catch (JSONException e) {
            e.printStackTrace();
        }

Fixed it used this code below it works flawlessly!!

        list.put("guest_login");
        try {
            obj.put("chips", new JSONObject().put("1", "2000"));
            obj.put("invested", new JSONObject().put("1", "2000"));
            obj.put("login_id", "10");
            JSONArray news = new JSONArray();
            news.put("Welcome to Poker.");
            news.put("Register and get started with 2000 play chips.");
            obj.put("news", news);   
            obj.put("username","Guest10");
            obj.put("epoch", "119677");
            list.put(obj);
        } catch (JSONException e) {
            e.printStackTrace();
        }
Sign up to request clarification or add additional context in comments.

2 Comments

wow thanks, this library should be built in!!! dam what a waste of time I did with Simple JSON
How would I go about translating the whole thing.. the chips gets chopped off with double }}'s but the epoch is fine.
0

I suggest you use POJOs. You can use this online tool http://pojo.sodhanalibrary.com/. Once you have your POJO, let say Guest class, you can add whatever you want and if you still need JSONObject, you can get it like this:

Guest guest = new Guest();
guest.setChips(//Anything);

JSONObject jsonObj = new JSONObject( guest );

2 Comments

more classes i rather just hardcode the json in a string and append more json later on for complicated array jsons, no way to do it with any json library I would switch to another json library if its possible
Is it really necessary to use simple library?

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.