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
java - Put in variables from JSON Object - Stack Overflow
Get JSON values and convert them into Java variables - Stack Overflow
How to put java variable into JSON string? - 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 have not escaped properly. You make sure you do:
var somejson = "{ \"key1\": \"val1\",\"key2\": \"value2\"}";
The easier way would be to just convert an existing object to a string using JSON.stringify(). Would recommend this as much as possible as there is very little chance of making a typo error.
var obj = {
key1: "val1",
key2: "value2"
};
var json = JSON.stringify(obj);
If you want the string, not the object (note the ' instead of ")
var somejson = '{ "key1": "val1", "key2": "value2" }';
If you want a string declared with multiple lines, not the object (newline is meaningful in Javascript)
var somejson = '{'
+ '"key1": "val1",'
+ '"key2": "value2"'
+ '}';
If you want the object, not the string
var somejson = { "key1": "val1", "key2": "value2" };
If you want a string generically
var somejson = JSON.stringify(someobject);
Using org.json library:
try {
JSONObject jsonObject = new JSONObject("{\"phonetype\":\"N95\",\"cat\":\"WP\"}");
}catch (JSONException err){
Log.d("Error", err.toString());
}
To anyone still looking for an answer:
JSONParser parser = new JSONParser();
JSONObject json = (JSONObject) parser.parse(stringToParse);