How to post a data using dio to an array wrapped inside a json and inside that array is also a json - flutter

How to post a data using dio to an array wrapped inside a json and inside that array is also a json. the structure of response is like below :
{
"result":[
{
"title":"String",
"caption":"String"
"related":[
{
"relatedTitle":"String",
"relatedCaption":"String"
}
]
}
]
}
I want to post value to the relatedTitle and relatedCaption inside the related key.
Please help out in it.

try to encode your json like:
var body = {
"title":"String",
"caption":"String",
"related":[
{
"relatedTitle":"String",
"relatedCaption":"String"
}
]
};
var response = await dio.post(Url, data: jsonEncode(body),);

Related

how to create array in flutter apis

I want submit Selected answers array how can do this.
How to pass selected answer in this array
Flutter/Dart doesn't have arrays. Use lists instead.
Solution
In the api-response ,it's always formatted into either List of Map or Map of List.
List of Map is Like :
[
{
"name":"ruby",
"gender":"female"
},
{
"name":"otis",
"gender":"male"
},
]
And, Map of List is Like :
{
"data": [
"Professor",
"Berlin",
"Tokyo"
]
}
So to access them you have to use JsonDecode and then look the response and process that.
For Map response ....
var resMap = jsonDecode(response.body);
For List response (use key after resMap with key)...
var resMap = jsonDecode(response.body);
var infoList = resMap["data"];
Demo
class CatTypeController {
Future<void> getLeaves() async {
String url = "https://meowfacts.herokuapp.com/?count=2";
http.Response response = await http.get(Uri.parse(url));
if (response.statusCode == 200) {
Map rM = jsonDecode(response.body);
print(rM["data"]);
}
}
}
Response
Output
["In ancient Egypt, killing a cat was a crime punishable by death.","The way you treat kittens in the early stages of it's life will render it's personality traits later in life."]

React using axios await to fecth jsondata and store it in a variable

The data I am retrieving is in JSON format. Would like to store it in a JSON format in some class variable as well. As I want to send the json data to another component.
// Simple GET request using axios
axios.get(userDataURL)
.then((response) => {
// console.log(response.data);
jsondata =response.data.json;
console.log("2"+jsondata);
this.setState({ users: response.data.results })
} );
}```

Sequelize returns wrong format from JSONB field

My "Item" model has a JSONB column named "formula".
I want to get it either as Object or JSON string. But what it returns is a string without quoted keys that can't be parsed as JSON.
My code:
async function itemsRead (where) {
const items = await models.item.findAll({
where
})
console.log(items)
return items
}
And what I see and get is:
[
item {
dataValues: {
id: 123,
formula: '{a:-0.81, x:5.12}',
}
},
.
.
.
]
My mistake was in insert (create) phase. I had to pass the original formula object (and not JSON stringified or other string forms) to create()
let formula = {a:-0.81, x:5.12}
models.item.create({id:123, formula})

Fetch nested data from Internet

How to fetch nested json response ? I can fetch direct value but cant fetch nested values
{
"username":"aa",
"data":{
"key":"value",
"anotherKey":"anotherValue"
}
}
You have to first decode your json like this-
var respBody = jsonDecode(response.body);
Then you can use respBody["data"]["key"]

How to use Restangular when the service for a collection returns a dictionary instead of an array?

I'm in the early phases of the development of a client app to an existing REST service and I'm trying to decide what to use for server communication. So far I'm loving Restangular documentation, it seems really solid, but I'm worried it's not going to work with the service because the responses look something like this:
{
"0": {
"name": "John",
"total": 230,
"score": 13
},
"1": {
"name": "Sally",
"total": 190,
"score": 12
},
"2": {
"name": "Harry",
"total": 3,
"score": 0
},
"...": "..."
}
I can't find in the docs if something like this is supported or how am I supposed to handle this type of response. Has anyone tried? Any ideas?
I'm the creator of Restangular :).
You can use that response with Restangular. You need to use the responseInterceptor. I guess that your response looks like that when you're getting an array. So you need to do:
RestangularProvider.setListTypeIsArray(false)
RestangularProvider.setResponseExtractor(function(response, operation) {
// Only for lists
if (operation === 'getList') {
// Gets all the values of the property and created an array from them
return _.values(response)
}
return response;
});
With this, it's going to work :)
Please let me know if this worked out fr you
You could do this if if you attached a "then" statement to the promise that you return.
That is, instead of this:
return <RectangularPromise>;
you have this:
return <RectangularPromise>.then(function(response){return _.values(response);};
Essentially, it's the same as #mgonto's answer, but doing it this way means that you don't have to modify your whole Rectangular Service if you only need to do this for one endpoint. (Although, another way to avoid this is to create multiple instances of the service as outlined in the readme).
unfortunately listTypeIsArray is not described in readme, so I haven't tried this option
but I resolved similar problem as described here
https://github.com/mgonto/restangular/issues/100#issuecomment-24649851
RestangularProvider.setListTypeIsArray() has been deprecated.
If you are expecting an object then handle it within addResponseInterceptor
app.config(function(RestangularProvider) {
// add a response interceptor
RestangularProvider.addResponseInterceptor(function(data, operation, what, url, response, deferred) {
var extractedData;
// .. to look for getList operations
if (operation === "getList") {
// .. and handle the data and meta data
extractedData = data.data.data;
extractedData.meta = data.data.meta;
} else {
extractedData = data.data;
}
return extractedData;
}); });
If you are expecting string response then convert it to array.
app.config(function(RestangularProvider) {
RestangularProvider.addResponseInterceptor(function(data, operation, what, url, response, deferred) {
var newData = data;
if (angular.isString(data)) {
newData = [data]; //covert string to array
}
return newData;
}); });