If you are struggling to arrange the "'s the correct syntax would be

String jsonRequestString = "{\"access_code\" : \""+code+"\" , ";

Instead of formatting Json string manually, which takes alot of effort, consider using a library or util.

For ex (going to use Jackson library) :

Request re = new Request();
re.setCode(code);
...
ObjectMapper mapper = new ObjectMapper();
String jsonStr = mapper.writeValueAsString(re);
Answer from Suresh Atta on Stack Overflow
🌐
Stack Overflow
stackoverflow.com › questions › 51003739 › how-to-assign-json-to-a-variable-in-java
How to assign JSON to a variable in Java - Stack Overflow
June 24, 2018 - One can not assign valid JSON to a Java variable or object type. But how do we import valid JSON into our classes if we really want to? To do this, JSON must be converted to a JSON String. There are many tools online which do this for us.
Discussions

Convert JSON object to simple variables in Java - Stack Overflow
May be you are talking about JSON.parse look at this stackoverflow.com/questions/4935632/parse-json-in-javascript ... So you have your json as String and you want to access given property directly? More on stackoverflow.com
🌐 stackoverflow.com
April 1, 2015
Get JSON values and convert them into Java variables - Stack Overflow
What I would like to is to separate ... into two variables like in the example below. The firstName and lastName will be various, depending on what the server is sending to me. String personFirstName= "Jim"; String personLastName = "Smith"; ... Did you already search for existing libraries for handling JSON data in Java..... More on stackoverflow.com
🌐 stackoverflow.com
Java : How to assign Json formated String to Java String? - Stack Overflow
I have a big json string which i will be getting as a request from the UI , which will be converted to a String and parsed . I want to simulate the similar environment for testing locally , so fo... More on stackoverflow.com
🌐 stackoverflow.com
How am I able to to assign Json values (a few different data types) to variables to be able to use them in a Java Application? - Stack Overflow
I am completely new to JSON and the file I'm dealing with has lots of objects inside objects (and they're all different variable types e.g. String, Int, Boolean). I am trying to create a weather More on stackoverflow.com
🌐 stackoverflow.com
🌐
Team Treehouse
teamtreehouse.com › community › how-to-create-variables-based-on-a-json-file
How to create variables based on a json file ? (Example) | Treehouse Community
May 28, 2016 - My mistake was exactly what you said in previous comments. The right code is: String name = jsonData.getString("name"); String publisher = jsonData.getString("publisher"); String language = jsonData.getString("language");
🌐
Stack Overflow
stackoverflow.com › questions › 14790733 › java-how-to-assign-json-formated-string-to-java-string
Java : How to assign Json formated String to Java String? - Stack Overflow
... On the client-side, do a search and regex "replace all" of double-quotes into single quotes on the desired form field before actually sending the request. ... Actually, Java doesn't have verbatim string literals.
Top answer
1 of 2
1

You need to think from object perspective. Your outer most Object is some kind of wrapper which contains most other important objects.

I have tried to convert the JSON to appropriate object structure. I hope you get my point. Please add missing objects.

public class OuterMainObject {
    private String cod;
    private float message;
    private int cnt;
    List<WeatherObject> list = new ArrayList<WeatherObject>();
    private City city;

}

class City {

    private String id;
    private String name;
    private Coordinates coord;
    private String country;
}

class Coordinates {
    private double lat;
    private double lon;
}

class WeatherObject {
    private Date dt; 
    private Main main;
    private Weather weather;
    private Clouds clouds;
    private Wind wind;
    private Rain rain;
    private Sys sys;
    private String dt_txt;

}

As you can see I have one OuterMainObject and there are multiple objects within that outermainobject now if you try and map the objects from outside to inside you can easily relate them.

Update for comment We don't have to worry about how to detect and store the variables. There are libraries which does this for us. So for example Jackson is one of such library. Please refer to this link. This one has a nice tutorial about this conversion. All you need to focus is converting the JSON to appropriate object structure.

In your case following should do it.

OuterMainObject obj = mapper.readValue(jsonString, OuterMainObject.class);
2 of 2
0

try like this.....

JSONObject JOBJ= new JSONObject(response);

    JSONArray JS= JOBJ.getJSONArray("list");

    for (int i = 0; i < JS.length(); i++) {
         JSONObject c = JS.getJSONObject(i);

     JSONObject main= paramsArr.getJSONObject("main");
    System.out.println(main.getString("temp"));
        //like this...

    JSONArray JS2= JOBJ.getJSONArray("weather");
    for (int j = 0; j < JS2.length(); j++)
    {
     JSONObject param1 = JS2.getJSONObject(0);

    //now get required values by key
    System.out.println(param1.getInt("id"));
    }
    }
🌐
Camunda
forum.camunda.io › camunda 7 topics › discussion & questions
ExternalTaskService.complete() - how to set a variable of type JSON - Discussion & Questions - Camunda Forum
May 21, 2019 - result is a variable of type String String jsonString = “{…}” HashMap map = new HashMap result is of type Object ...
Find elsewhere
🌐
CopyProgramming
copyprogramming.com › howto › how-to-assign-json-to-a-variable-in-java
Java: Assigning JSON to a Java Variable: A Guide
June 3, 2023 - It's impossible to assign valid JSON to a Java variable or object type. However, if there's a need to import valid JSON into our classes, it must be converted into a JSON String.
Top answer
1 of 1
1

First, create a pojo class with different getter/setter for your required fields.

  public class MyResponse(){

    private List<String> idList;
    private List<String> nombreList;
    private List<String> descList;
    private List<String> califList;

    public void setIdList(List<String> list){
    this.idList = list;
    }

    public void getIdList(){
    return idList;
    }

    public void setNombreList(List<String> list){
    this.nombreList = list;
    }

    public void getNombreList(){
    return nombreList;
    }

    public void setDescList(List<String> list){
    this.descList = list;
    }

    public void getDescList(){
    return descList;
    }

    public void setCalifList(List<String> list){
    this.califList = list;
    }

    public void getCalifList(){
    return califList;
    }

}

Now parse each response and add in the list of MyResponse type

         public MyResponse getDataJSON(String response)
                    {
                        MyResponse myResponse = new MyResponse();
                        ArrayList<String> idList = new ArrayList<String>();
                        ArrayList<String> nombreList = new ArrayList<String>();
                        ArrayList<String> descList = new ArrayList<String>();
                        ArrayList<String> califList = new ArrayList<String>();

                        try
                        {
                            JSONArray jsonArray = new JSONArray(response);

                            for(int i=0; i<jsonArray.length(); i++)
                            {
                              idList.add(jsonArray.getJSONObject(i).getString("id"));

   nombreList.add(jsonArray.getJSONObject(i).getString("nombre"));
                              descList.add(jsonArray.getJSONObject(i).getString("descripcion"));
                              califList.add(jsonArray.getJSONObject(i).getString("calificacion"));
                            }

                            myResponse.setIdList(idList);
                            myResponse.setNombreList(nombreList);
                            myResponse.setDescList(descList);
                            myResponse.setCalifList(califList);

                            return myResponse;
                        }

                        catch (Exception e)
                        {
                            e.printStackTrace();
                        }

                        return list;
                    }

To retrieve back the data from getDataJSON()

     MyResponse myResponse = getDataJSON(new String(responseBody)));
     ArrayList<String> idList = myResponse.getIdList();
     ArrayList<String> nombreList = myResponse.getNombreList();
     ArrayList<String> descList = myResponse.getDescList();
     ArrayList<String> califList = myResponse.getCalifList();
🌐
ServiceNow Community
servicenow.com › community › developer-forum › pass-variable-value-to-json-object › m-p › 1608224
Solved: Pass variable value to json object - ServiceNow Community
July 27, 2022 - Or at least when assigning it to your literal object, assign as a string. ... var json = { "opened_by":"62826bf03710200044e0bfc8bcbe5df1", "requested_for": newReferent.toString(), "department":"221f3db5c6112284009f4becd3039cc9" }; Depending upon how and where you are assigning newReferent the type is getting skewed. ... Make changes in variable set client script so it does not affect other catalog items in Developer forum yesterday
🌐
Stack Overflow
stackoverflow.com › questions › 43485994 › json-assign-java-string
JSON assign Java String - Stack Overflow
My json structure is { "GAME_CUSTOMIZE": { "GAME_CODE": "MOCK12V2.0", "RESULT": { "response": [ { "id": "PLAYER1", ...
🌐
Javatpoint
javatpoint.com › convert-json-file-to-string-in-java
Convert JSON File to String in Java - Javatpoint
Convert JSON File to String in Java with java tutorial, features, history, variables, object, programs, operators, oops concept, array, string, map, math, methods, examples etc.
🌐
Stack Overflow
stackoverflow.com › questions › 61861194 › how-to-put-java-variable-into-json-string
How to put java variable into JSON string? - Stack Overflow
May 18, 2020 - One way: 1. Parse the JSON String to its representative Java object. 2. Change the fields of this object as you desire.
🌐
freeCodeCamp
freecodecamp.org › news › jsonobject-tostring-how-to-convert-json-to-a-string-in-java
JSONObject.toString() – How to Convert JSON to a String in Java
April 14, 2023 - In Java, the JSONObject class provided by the org.json package is commonly used to create and manipulate JSON objects. The JSONObject.toString() method is a useful method provided by this class that converts a JSON object to a string representation.
🌐
UiPath Community
forum.uipath.com › help › studio
How to Add a variable to json config - Studio - UiPath Community Forum
April 27, 2022 - If v2 is a variable, replace that with {0} and while deserializing, use this: String.Format( str_JsonString, VariableName) · Suppose the JsonString is this: {{ “key”:“value”, “key2”:“value2”, “key3”:[ {{ “inner1”:“{0}”, ...