how can get dynamic key json in flutter - flutter

"
success": true,
"result": {
"values": {
"asdf": [],
"dj": [
{
"id": 18,
"ownerId": "5b0b3932-e262-4ac4-923c-13daf2bd4a3c",
"ownerName": "tester",
"name": "masr",
"description": "she was and firebase have also had to make their decision and make the beg to 8be 8the 8same 6th 7century 8of 8and 6and 88th century ones in flutter take on my favourite ",
"statusId": "PENDING",
"status": null,
"price": 9000.00,
"isPublic": false,
"startDate": "2022-05-26T00:00:00",
"expectedEndDate": "2022-05-27T00:00:00",
"finishDate": null,
"interests": [
{
"id": "my-first-interest",
"isDeleted": false
},
{
"id": "gdg",
"isDeleted": false
},
{
"id": "dj",
"isDeleted": false
}
]
},
]
}
}
the dynamic key is asdf and dj change form user to anoter
i want to get id or ownername .... etc without object can any one help me in this cause

Since you don't know the key name in advance, you will have to iterate over all of them looking for JSON object members that look like they contain ID and owner. Something like this:
import 'dart:convert';
import 'dart:io';
void main() {
final decoded = json.decode(File('json1.json').readAsStringSync());
// values will be a JSON object
final values = decoded['result']['values'] as Map<String, dynamic>;
// values.values will be all of the JSON arrays in that object
// do a whereType just to rule out any other fields in the JSON object
// use expand to merge all lists together
// and wheretype again to double check that we only have JSON objects
// further check that only JSON objects with the right values are selected
// and map these to PODOs
final result = values.values
.whereType<List>()
.expand((e) => e)
.whereType<Map>()
.where((e) => e.containsKey('id') && e.containsKey('ownerId'))
.map<IdAndOwner>((m) => IdAndOwner(m['id'], m['ownerId']))
.toList();
print(result); // prints [Id/OwnerId=18/5b0b3932-e262-4ac4-923c-13daf2bd4a3c]
}
class IdAndOwner {
final int id;
final String ownerId;
IdAndOwner(this.id, this.ownerId);
#override
String toString() => 'Id/OwnerId=$id/$ownerId';
}

Related

Cast to model List<Map<String,dynamic>> issue flutter

I want convert my response API to model repository in flutter but always get error in type of model
use Dio for api request
this is my response API :
{
"success": 1,
"list": [
{
"id": "1",
"driver_id": "11081",
"company_id": "9",
"school_id": "4",
"status": 1,
"createdAt": "2022-10-14T09:22:00.000Z",
"updatedAt": "2022-10-14T09:22:03.000Z"
},
{
"id": "2",
"driver_id": "11081",
"company_id": "9",
"school_id": "5",
"status": 1,
"createdAt": "2022-10-14T20:36:47.000Z",
"updatedAt": "2022-10-14T20:36:49.000Z"
}
]
}
I try get data like this :
Future<List<ServiceEntity>> getAll() async {
final List<ServiceEntity> services = [];
try {
final response = await httpClient.post("v1/teacher/get-list");
validateResponse(response);
(response.data['list'] as List).forEach((jsonObject) {
services.add(ServiceEntity.fromJson(jsonObject));
});
} on DioError catch (e) {
exceptionHttpRequest(e);
}
return services;
}
and model is :
class ServiceEntity {
final int id;
ServiceEntity.fromJson(Map<String, dynamic> json)
: id = json['id'];
}
When i build app and debug always get this error :
I/flutter (29931):
type 'String' is not a subtype of type 'int'
I test many things but in fact did not understand what is the issue
The problem is exactly what the error says. String is not a subtype of int.
Look at the data:
The "id" is stored as a String.
Then you try pull it out in the ServiceEntitys fromJson to an int.
a. Either just store the value as an int, because that's what it is, just b like the success field.
b. or parse the string into an int.
Below an example with chatty printing to explain the type changes.
const data = {
"id": "1",
};
try{
// value stored as a String
final value = data["id"] as String;
print(value.runtimeType);
print(value);
// parse into an int
final id = int.tryParse(value);
print(id.runtimeType);
print(id);
}catch(e){
print("ERROR ${e}");
}
Your id data type is String on response, to use it as int on model class you can parse to int,
id = int.tryParse(json['id']) ?? 0;
if parser failed, it will get 0.
More about converting json and tryParse

Flutter : How change modify unmodifiable map

I have list like this in provider:
List orders=[]:
void getOrders(){
orders = [
{"id":1,
"order":[
{"id":1,"name":"mike"},
{"id":2,"name":"john"},
{"id":3,"name":"smith"}
]
},
{"id":1,
"order":[
{"id":1,"name":"roz"},
{"id":2,"name":"sam"},
{"id":3,"name":"ruby"}
]
},
];
notifyListeners();
}
in provider when I use this methos to chane indexed order with another:
void changeOrder(orderIndex,item){
orders[orderIndex].update("order",(val)=>item);
notifyListeners();
}
I get this error type '(dynamic) => dynamic' is not a subtype of type '(Object) => Object' of 'update'
and when I use this :
void changeOrder(orderIndex,item){
orders[orderIndex]["order"]=item;
notifyListeners();
}
I get this error Unsupported operation: Cannot modify unmodifiable map
Add More Details
the item in changeOrder method comes from screen contain orders :
var item = List.from(orders[index]);
orders type is List<Map<String, dynamic>>. While reading the item, it will be a map instead of list.
Map item = Map.from(orders[index]);
And you can use both way you;ve tried.
List<Map<String, dynamic>> orders = [];
void getOrders() {
orders = [
{
"id": 1,
"order": [
{"id": 1, "name": "mike"},
{"id": 2, "name": "john"},
{"id": 3, "name": "smith"}
]
},
{
"id": 1,
"order": [
{"id": 1, "name": "roz"},
{"id": 2, "name": "sam"},
{"id": 3, "name": "ruby"}
]
},
];
}
void changeOrder(orderIndex, item) {
orders[orderIndex]["order"] = item;
// orders[orderIndex].update("order", (val) => item);
}
void main(List<String> args) {
getOrders();
print(orders);
Map item = Map.from(orders[1]);
changeOrder(1, item);
print(orders);
}

Add json values in to the list of object

How I can get all the values in this JSON and add all the value in to the list of object in the dart?
"data": [
{
"$id": "2",
"serviceId": 1017,
"name": "اکو",
"code": "235",
"price": 1562500,
"isDefault": true,
"transportCostIncluded": false,
"qty": 0,
"minQty": 1,
"maxQty": 2,
"description": "یک دستگاه اکو به همراه دو باند و یک عدد میکروفن (تامین برق بعهده پیمانکار می باشد).",
"contractorSharePercent": 65,
"unitMeasureId": 7,
"unitMeasureName": "هر 60 دقیقه",
"superContractorsId": null
},
],
like this var list = ["2",1017,....]
Assuming you've a JSON file, which you may have parsed like this:
String json = await rootBundle.loadString('file_name.json');
var response = jsonDecode(json);
This is how you can do it:
List<dynamic> jsonData; //similar to var jsonData = [something, something, ...]
//traversing through each value in the key value arrangement of the json
for (var k in response.values) {
jsonData.add(k); //adding each value to the list
}
After the loop ends, jsonData will have all the values of your JSON file.
It's important for you to know that even if you put the keys on a list, they won't necessarily be in order, because of the way maps work.
Assuming your json is a map and not a json string, you could put all of the values on a list like so:
var myList = (jsonObject['data'] as List).fold<List>(
[],
(prev, curr) => [...prev, ...curr.values]
);
if you were talking about a json string:
Map<String, dynamic> jsonObject = jsonDecode(jsonString);
For simplicity, lets assume this json is unparsed in a string.
(1) Assuming the code snippet you added is a string && is valid json you can do as follows :)
import 'dart:convert';
void main() {
var x = json.decode('''
{
"data": [
{
"hello": "2",
"serviceId": 1017,
"name": "اکو",
"code": "235",
"price": 1562500,
"isDefault": true,
"transportCostIncluded": false,
"qty": 0,
"minQty": 1,
"maxQty": 2,
"description": "یک دستگاه اکو به همراه دو باند و یک عدد میکروفن (تامین برق بعهده پیمانکار می باشد).",
"contractorSharePercent": 65,
"unitMeasureId": 7,
"unitMeasureName": "هر 60 دقیقه",
"superContractorsId": null
}
]
}
''');
print(x);
List<dynamic> listOfObjects = (x['data'] as Iterable<dynamic>).toList();
/// this is just gonna be a list of all of your object.
print(listOfObjects);
List<dynamic> listOfValues = (x['data'] as Iterable<dynamic>).map((_object) {
return (_object as Map<String, dynamic>).values.toList();
}).toList();
/// this is gonna be a 2d array here,
print(listOfValues);
}
Hope This helped out:)
Also json here comes from import 'dart:convert';

Difficulty in parsing JSON

I want to develop a flutter app and I am finding it difficult to parse this JSON I have seen posts regarding this but I am finding it difficult to parse this. Help a beginner out! Here is the JSON link " https://api.covid19india.org/state_district_wise.json "
I am simply using:
Future getJsonDistrictData() async {
var response = await http.get(Uri.encodeFull(url1));
var converttojson1 = json.decode(response.body);
setState(() {
myData= converttojson1;
});
}
Text("${myData['Kerala']['districtData']['Thrissur']['confirmed']}")
But this would mean I have to write the same line for every district confirmed cases.
How should I proceed?
I have made the following example of an implementation you can use the parse the data from the URL in your question.
It can be simplified if you don't need the lastUpdatedTime and delta_confirmed since it looks like this is always the empty String and zero in the API.
import 'dart:convert';
class State {
final Map<String, DistrictData> districts = {};
State.fromJson(dynamic json) {
for (final district in json['districtData'].keys) {
districts[district as String] =
DistrictData.fromJson(json['districtData'][district]);
}
}
#override
String toString() => districts.toString();
}
class DistrictData {
final int confirmed;
final DateTime lastUpdatedTime;
final int delta_confirmed;
DistrictData(this.confirmed, this.lastUpdatedTime, this.delta_confirmed);
factory DistrictData.fromJson(dynamic json) => DistrictData(
json['confirmed'] as int,
(json['lastupdatedtime'].isEmpty as bool)
? null
: (DateTime.parse(json['lastupdatedtime'] as String)),
json['delta']['confirmed'] as int);
#override
String toString() =>
'{ confirmed: $confirmed, lastUpdatedTime: $lastUpdatedTime, delta_confirmed: $delta_confirmed }';
}
void main() {
final states = json.decode(input).map(
(String key, dynamic value) => MapEntry(key, State.fromJson(value)))
as Map<String, State>;
states.forEach((key, value) => print('$key : $value'));
/*
Kerala : {Thrissur: { confirmed: 13, lastUpdatedTime: null, delta_confirmed: 0 }, Alappuzha: { confirmed: 5, lastUpdatedTime: null, delta_confirmed: 0 }}
Delhi : {East Delhi: { confirmed: 1, lastUpdatedTime: null, delta_confirmed: 0 }}
*/
}
const input = '''
{
"Kerala": {
"districtData": {
"Thrissur": {
"confirmed": 13,
"lastupdatedtime": "",
"delta": {
"confirmed": 0
}
},
"Alappuzha": {
"confirmed": 5,
"lastupdatedtime": "",
"delta": {
"confirmed": 0
}
}
}
},
"Delhi": {
"districtData": {
"East Delhi": {
"confirmed": 1,
"lastupdatedtime": "",
"delta": {
"confirmed": 0
}
}
}
}
}
''';
Copy your JSON in this link(https://javiercbk.github.io/json_to_dart/) and get model Class.
Example:
For example your Model Class Name is CovidModel. As you get response from network call, pass data to you model class and get object like this:
CovidModel _covidModel = CovidModel.fromJson(response.data);
now you can use _covidModel to get inner objects and show result in app page.

Rearrrange populated json result in mongoose

A simple json response for Post.find().populate("name") will return json result as follow. Note: The focus of the question is to rearrange the "name":"Alex" in json to the final structure as shown. Ignore the part that need hiding _id and __v. Thanks.
[
{
"_id": "54cd6669d3e0fb1b302e54e6",
"title": "Hello World",
"postedBy": {
"_id": "54cd6669d3e0fb1b302e54e4",
"name": "Alex",
"__v": 0
},
"__v": 0
},
...
]
How could i rearrange and display the entire json as follow?
[
{
"_id": "54cd6669d3e0fb1b302e54e6",
"title": "Hello World",
"name": "Alex"
},
...
]
You can use the lean() method to return a pure JSON object (not a mongoose document) that you can then manipulate using lodash helper methods such as map(), like in the following example:
Post.find()
.populate("name")
.lean().exec(function (err, result) {
if(result){
var posts = _.map(result, function(p) {
p.name = p.postedBy.name;
p.postedBy = undefined;
return p;
});
console.log(posts);
}
});
You can disable the "__v" attribute in your Schema definitions by setting the versionKey option to false. For example:
var postSchema = new Schema({ ... attributes ... }, { versionKey: false });
As follow-up to your question on rearranging the order of the properties in the JSON, JS does not define the order of the properties of an object. However, you
can use both the JSON.parse() and JSON.stringify() methods to change the order, for example
var json = {
"name": "Alex",
"title": "Hello World",
"_id": "54cd6669d3e0fb1b302e54e6"
};
console.log(json);
//change order to _id, title, name
var changed = JSON.parse(JSON.stringify(json, ["_id","title","name"] , 4));
console.log(k);
Check the demo below.
var json = {
"name": "Alex",
"title": "Hello World",
"_id": "54cd6669d3e0fb1b302e54e6"
};
//change order to _id, title, name
var changed = JSON.parse(JSON.stringify(json, ["_id","title","name"] , 4));
pre.innerHTML = "original: " + JSON.stringify(json, null, 4) + "</br>Ordered: " + JSON.stringify(changed, null, 4);
<pre id="pre"></pre>