I am Logging in to the website using this block of code and printing the status code as well as the response body in the console.
void sendLogin() async {
var map = <String, dynamic>{
"UserName": _usernameController.text,
"Password": _passwordController.text,
};
var res = await http.post(
Uri.parse("http://192.168.1.8:8080/HongLeong/LOGIN_REQUEST.do"),
body: map,
);
print(res.statusCode);
print(res.body);
}
and this is what is being printed in the console
I/flutter ( 9739): null
I/flutter ( 9739): 200
I/flutter ( 9739): {"RESPONSE":{"BASEL_RESPONSE":{"UserDate":"0","UserTime":"0","module_config_1":"0","module_config_2":"0","ErrEntity":{"MessageID":"1110","Message":"Field Code is mandatory","last_req_id":"50029","table_id":"0","operation_id":"0"},"is_csv":"0","VersionName":"DYMA @ 6.1.24.0, ORG @ 2017.3.22.15.0.41, GRC @ 2017.3.22.15.0.55, LDC @ 2017.3.22.15.1.8, DYMA_XML @ 2017.3.22.15.0.30, NAS @ 2017.3.22.15.1.22 - Config: 0 - Node: OPRISK_DATACOLLECTOR","ExpiryDate":"31/01/2030","count_key":"0","id_Us":"0","is_popup":"0","tot_messages":"0","my_messages":"0","product":"0"},"RESPONSE_HEADER":{"NomeRichiesta":"LOGIN_REQUEST","ltimeStart":"23531871","ltimeStop":"23531884","ldate_null":"19900101","product":"1","server_name":"OPRISK_DATACOLLECTOR","cell_context_id":"538058","operation_key":"1000000","operation_sub_num":"-1"}}}
How do I access then print the ErrEntity with its contents from the response body
"ErrEntity":{"MessageID":"1110","Message":"Field Code is mandatory","last_req_id":"50029","table_id":"0","operation_id":"0"}
heres a better view of the response body in formatted Json
{
"RESPONSE":{
"BASEL_RESPONSE":{
"UserDate":"0",
"UserTime":"0",
"module_config_1":"0",
"module_config_2":"0",
"ErrEntity":{
"MessageID":"1110",
"Message":"Field Code is mandatory",
"last_req_id":"50029",
"table_id":"0",
"operation_id":"0"
},
"is_csv":"0",
"VersionName":"DYMA @ 6.1.24.0, ORG @ 2017.3.22.15.0.41, GRC @ 2017.3.22.15.0.55, LDC @ 2017.3.22.15.1.8, DYMA_XML @ 2017.3.22.15.0.30, NAS @ 2017.3.22.15.1.22 - Config: 0 - Node: OPRISK_DATACOLLECTOR",
"ExpiryDate":"31/01/2030",
"count_key":"0",
"id_Us":"0",
"is_popup":"0",
"tot_messages":"0",
"my_messages":"0",
"product":"0"
},
"RESPONSE_HEADER":{
"NomeRichiesta":"LOGIN_REQUEST",
"ltimeStart":"23531871",
"ltimeStop":"23531884",
"ldate_null":"19900101",
"product":"1",
"server_name":"OPRISK_DATACOLLECTOR",
"cell_context_id":"538058",
"operation_key":"1000000",
"operation_sub_num":"-1"
}
}
}
Thanks for the help :)
You need to use the dart:convert
library to convert the JSON response into a Map
. Then you can access the key/value pairs as your would with a regular Map
. You'll also have to add a cast (in my example at least) for the correct type using the as
keyword.
For example:
import 'dart:convert';
void main() {
final s = """
{
"RESPONSE":{
"BASEL_RESPONSE":{
"UserDate":"0",
"UserTime":"0",
"module_config_1":"0",
"module_config_2":"0",
"ErrEntity":{
"MessageID":"1110",
"Message":"Field Code is mandatory",
"last_req_id":"50029",
"table_id":"0",
"operation_id":"0"
},
"is_csv":"0",
"VersionName":"DYMA @ 6.1.24.0, ORG @ 2017.3.22.15.0.41, GRC @ 2017.3.22.15.0.55, LDC @ 2017.3.22.15.1.8, DYMA_XML @ 2017.3.22.15.0.30, NAS @ 2017.3.22.15.1.22 - Config: 0 - Node: OPRISK_DATACOLLECTOR",
"ExpiryDate":"31/01/2030",
"count_key":"0",
"id_Us":"0",
"is_popup":"0",
"tot_messages":"0",
"my_messages":"0",
"product":"0"
},
"RESPONSE_HEADER":{
"NomeRichiesta":"LOGIN_REQUEST",
"ltimeStart":"23531871",
"ltimeStop":"23531884",
"ldate_null":"19900101",
"product":"1",
"server_name":"OPRISK_DATACOLLECTOR",
"cell_context_id":"538058",
"operation_key":"1000000",
"operation_sub_num":"-1"
}
}
}
""";
final data = jsonDecode(s);
print((data as Map)['RESPONSE']['BASEL_RESPONSE']['ErrEntity']);
}
Prints:
{MessageID: 1110, Message: Field Code is mandatory, last_req_id: 50029, table_id: 0, operation_id: 0}
In your case, you'll ned to use:
final data = jsonDecode(res.body);
to convert the data.
I do though recommend that you use a Class model instead. You can use quicktype.io to generate one for you automatically.
See: