I have an object that is created by Newtonsoft's JSON serializer. I need to get the JSON string that was used to create the object. How do I serialize the object into a simple JSON string?
Try this:
public string jsonOut()
{
// Returns JSON string.
return JsonConvert.SerializeObject(this);
}
Related
I have response body with type _JsonMap
How I can convert it into standard Json? I cannot find way to extract map with my data from this object
Dart has a built-in library called dart:convert which lets you convert data types.
You can use jsonDecode(String source) function to convert JSON strings to dart map
jsonDecode function
dynamic jsonDecode(
String source,
{Object? reviver(
Object? key,
Object? value
)?}
)
Parses the string and returns the resulting Json object.
import 'dart:convert';
...
final Map map = jsonDecode(response.body);
If you are trying to parse a JSON that is not in string format
you can just use
final Map map = Map.from(response.body);
final Map map = Map.from(response.body);
answer from #Terblanche Daniel
worked for me
I have a String (jsonData) being mapped to json via Jackson Object mapper as below to JaxB.
var completeJson = objectMapper.readValue(jsonData, Data.class);
myDto.setFirstName(completeJson.getFirstName())
myDto.setLastName(completeJson.getLastName()))
.....
.....etc..etc
I'm able to map to the above strings just fine. However, I'm having problems mapping
to a jooq JSON object. I guess I will now have to convert jsonData to jooq JSON.
How would I do this?
JSON newJson = objectMapper.(best method to use);
myDto.setJsonSource(newJson)
Or maybe I have to create some sort of wrapper?
DTO configured by jooq
public myDto setJsonSource(JSON jsonSource) {
this.jsonSource = jsonSource;
return this;
}
The org.jooq.JSON type is just a wrapper around a string, so why not just use:
JSON json = JSON.json(objectMapper.writeValueAsString(object));
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.
I m using Jaql to manipulate data(data stored in json converted to string). The output for Jaql will be a com.ibm.jaql.json.type.JsonValue. It can be stored into string through toString() method. I got to do some json manipulations on the returned value. Problem here is, the output is not actual simple JSONObject. Is there a way to transform my string or JsonValue to JSONObject???
Please help.
Just use the String constructor of JSONObject:
JSONObject json = new JSONObject(jsonString);
I have been trying to evaluate GWT Autobean feature to decode/encode JSON object to domain objects for REST calls.
Following the example : http://code.google.com/p/google-web-toolkit/wiki/AutoBean#Quickstart
I was able to convert a singular JSON object to a domain object:
AutoBean<Person> personBean = AutoBeanCodex.decode(factory, Person.class, JsonResources.INSTANCE.json().getText());
where JsonResources.INSTANCE.json() is returning a JSON string.
However, I haven't been successful to convert a list of Person objects from JSON.
It would be helpful, if anyone has an example of this?
Thanks!
Well the only way I can think of is to create a special autobean, which will have List<Person> property. For example:
public interface Result {
void setPersons(List<Person> persons);
List<Person> getPersons();
}
And example json string:
{
persons:[
{"name":"Thomas Broyer"},
{"name":"Colin Alworth"}
]
}
UPDATE:
Workaround when input JSON is an array ( as suggested by persons[0] in comments).E.g. JSON looks like this:
[{"name":"Thomas Broyer"},{"name":"Colin Alworth"}]
And parsing code looks like this:
AutoBeanCodex.decode(factory, Result.class, "{\"persons\": " + json + "}").getPersons();