Codename One converting String to JSON object - json

How do you convert this string to JSON data in codenameone.
{result_code=0.0, data=[{id=1007747505, name=Test, dob=1986-05-16, identification=6837904, otherinformation=, status=Active, classid=11, class=Twenty, start_date=2016-05-27, end_date=2017-05-26}], message=OK}

You can use the JSONParser class as described in the developer guide using:
JSONParser p = new JSONParser();
Map<String, Object> results = p.parseJSON(new CharArrayReader("{result_code=0.0, data=[{id=1007747505, name=Test, dob=1986-05-16, identification=6837904, otherinformation=, status=Active, classid=11, class=Twenty, start_date=2016-05-27, end_date=2017-05-26}], message=OK}".toCharArray());
The trick is to place a breakpoint after that line and look at the contents of "results" to see the data structure returned by the parser.

Related

What is the standard for communicating in JSON when you're putting it out and not bringing it in?

I am told I need to communicate in flutter over the web using JSON. I know how to bring in JSON data and convert it into a Dart object. How am I expected to put out the JSON? Should I output the JSON in the form of a Dart object? How does that work? I've tried to do research but can't seem to find the answer.
You can add a method to your class that serialized it into a dictionary like this:
class Car {
final int nWheels;
final String color;
Car(this.nWheels, this.color);
Map<String, dynamic> toMap() => {
"nWheels": this.nWheels,
"color": this.color,
}
}
The resulting map can then be converted to a JSON string by using the flutter json library. That would look like this:
Car car = Car(4, "blue-ish");
String json = jsonEncode(car.toMap());
json is now a JSON-encoded String that can be transmitted to the server.

GSON: converting a .json file to a JsonObject

Of course, for this i used JsonParser, Here is my Code:
JsonParser parser = new JsonParser();
Object object = parser.parse(new FileReader("c:\\Projects\\elastic-search-single-log-result.json"));
JsonObject jobject = (JsonObject) object;
String message = jobject.get("msg").toString();
return message;
However, the msg is a stack trace enclosed by triple quotes, and it gives me a Malformed Json Exception, at the second line shown above.
I saw stuff about JsonReader having a getLenientMethod, I was wondering if there was something similar for this.
I fixed it --- I simply had to remove the triple quotes, and I get the JSON file from kibana (which formatted the stack trace a little differently). I hope this helps anyone in the future who will have the same problem

How to remove escaping slashes from a json inside another json?

I have the following json, that is being populated by:
new Gson().toJson(batch.getProducts());
But when im adding it to my main JSONObject, the fact that it's a value of a json is doing all the json (even the keys) be escaped.
{"name":"Ufud","store":2,"products":"[{\"name\":\"Test2\",\"stock\":2,\"value\":19.9},{\"name\":\"Test2\",\"stock\":2,\"value\":19.9},{\"name\":\"Teste2\",\"stock\":2,\"value\":19.9}]"}
Is there any method to prevent this?
If you're going to manipulate your json in future you may consider converting it not into a string, but into a JsonElement which can be quite easily added to already existing JsonObject
JsonObject mainObject = getMainObject(); //method which creates your main object. You can change it to variable reference
JsonElement productsElement = new Gson().toJsonTree(batch.getProducts()); //converting your object into JsonElement
mainObject.add("products", productsElement); //insering products element into object.
String json = new Gson().toJson(mainObject); //converting object with products element into a json string

Get JSON value from JSON which is type string in Mule data mapper

I am using Mule 3.6.1 and in datamapper I have a JSON object which is a string datatype and I need to get the value of a field from the JSON object.
How can I get this value from the JSON object while the object is of type String?
I cannot use the JSON transformer for this.
Thanks for any help
To convert a String of JSON and get one of its field value inside DataMapper, then you can utilize code like this (in DataMapper Script area):
jsonObject = new org.json.JSONObject(input.jsonstring);
output.jsonValue = jsonObject.getString("jsonfield");
In order to convert JSON element to a series of objects. Google GSon library is very helpful.
Example:
import com.google.gson.Gson;
Gson gson = new Gson();
Student studentTest = gson.fromJson(data, Student.class);
System.out.println("Amount: " + studentTest .getStudentName());

JSON, Servlet, JSP

Firstly, my HTTP POST through a URL accepts 4 parameters. (Param1, Param2, Param3, Param4).
Can I pass the parameters from the database?
Once the URL is entered, the information returned will be in text format using JSON
format.
The JSON will return either {"Status" : "Yes"} or {"Status" : "No"}
How shall I do this in servlets? doPost()
Just set the proper content type and encoding and write the JSON string to the response accordingly.
String json = "{\"status\": \"Yes\"}";
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
response.getWriter().write(json);
Instead of composing the JSON yourself, you may consider using an existing JSON library to ease the job of JSON (de)serializing in Java. For example Google Gson.
Map<String, String> result = new HashMap<String, String>();
result.put("status", "Yes");
// ... (put more if necessary)
String json = new Gson().toJson(result);
// ... (just write to response as above)
Jackson is another option for JSON object marshalling.