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 OverflowIf 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);
String yourVariable = "xyz";
String jsonRequestString = "{\"access_code\" : \"" + yourVariable + "\" , "
+ "\"merchant_reference\" : \"123\", \"language\" : \"en\",\"id\" : \"149018273\","
+ "\"merchant_identifier\" : \"gKc\", \"signature\" : \"570fd712af47995468550bec2655d9e23cdb451d\", "
+ "\"command\" : \"VOID\"}";
Convert JSON object to simple variables in Java - Stack Overflow
Get JSON values and convert them into Java variables - Stack Overflow
Java : How to assign Json formated String to Java String? - Stack Overflow
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
If you don't want to directly convert your input to the expected Object you could create a JSONObject from your input like
JSONObject root = new JSONObject(input);
and then navigate from there exactly to the attribute you need e.g.:
root.getJSONArray("results").getJSONObject(0).getString("is_structured");
EDIT: (receive value under date)
root.getJSONArray("results").getJSONObject(0).getJSONObject("date").getString("value");
You can have just the fields you need in your bean definition. For example, given the following JSON
{
firstName: 'It',
lastName: 'works',
ignoreMe: '!!!'
}
the bean definition does not contain ignoreMe:
public class Contact {
public String firstName; // setter and getter
public String lastName; // setter and getter
}
But it works with Gson:
Gson gson = new Gson();
Contact contact = gson.fromJson(new FileReader(new File("test.json")), Contact.class);
System.out.println("It worked!");
I suggest to use one of many JSON processing libraries available.
Examples are Gson and Jackson.
Both of them allow to convert: json string <--> object
You should start with defining domain model:
public class Person {
private String firstName;
private String lastName;
// getters/setters
}
Then you should do like this (for example, for GSON):
String json = "{\"firstName\":\"Jim\",\"lastName":\"Smith\"}";
Gson gson = new Gson();
Person person = gson.fromJson(json, Person.class);
Hope this helps
An example for you:
Read Json into str (a string value), then:
JSONObject obj= new JSONObject(str);
String personFirstName = obj.getString("firstName");
String personLastName = obj.getString("lastName");
hope this help!
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);
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"));
}
}