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
Related
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 am trying to get a map of columnName->values from a dataframe.I tried
val g=dataFrame.limit(limit)
val p=g.columns.map(i=>(i,g.select(i).map(_.get(0)).collect()))
and
val g=dataFrame.limit(limit)
val p=g.columns.map(i=>(i->g.select(i).map(_.get(0)).collect()))
But bot gives me an Array[String,Array[Any]]
I want to get a map[String,Array[Any]]
I also tried .toMap at the end to convert array to map,
val g=dataFrame.limit(limit)
val p=g.columns.map(i=>(i,g.select(i).map(_.get(0)).collect())).toMap
val gson=new Gson
gson.toJson(p)
but this gives me json string of the form
{"key1":"eq_site_deductible","value1":[0.0,0.0,0.0,],"key2":"county","value2":["CLAY COUNTY","CLAY COUNTY","Mary county"]}
I want to get a json string of the form {"eq_site_deductible":[value array],"county":[value array]}
If you just need the json, you don't need to convert it into map.
Below snippet can be used to write the dataframe content into a json file
dataFrame.write.format("json").save("result.json")
Or if you need the json string to be processed further in your code, you can use dataframe.toJSON to get the RDD[String], in which String will be the json
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.stringify(obj) converts the object into normal JSON format.
How to convert a javascript object to DyanamoDb JSON format ?
Not sure what you mean by "DynamoDB JSON format".
JSON, is JSON, but here is an example of putting a JSON object into DynamoDB:
Document doc = Document.FromJson( itemJson );
table.PutItem( doc);
This library does exactly what I wanted to do:
"Translates sane javascript objects (and JSON) into DynamoDb format and vice versa."
https://www.npmjs.com/package/dynamodb-marshaler
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);
}