how can i parse json weather response - json

this is my response from weather map api
{
"message": "accurate",
"cod": "200",
"count": 3,
"list": [
{
"id": 2641549,
"name": "Newtonhill",
"coord": {
"lat": 57.0333,
"lon": -2.15
},
"main": {
"temp": 275.15,
"pressure": 1010,
"humidity": 93,
"temp_min": 275.15,
"temp_max": 275.15
},
"dt": 1521204600,
"wind": {
"speed": 9.3,
"deg": 120,
"gust": 18
},
"sys": {
"country": ""
},
"rain": null,
"snow": null,
"clouds": {
"all": 75
},
"weather": [
{
"id": 311,
"main": "Drizzle",
"description": "rain and drizzle",
"icon": "09d"
}
]
}
how can i get the description i did get the temperature using a retrofit by seriazable object but i could'nt get the weather description
i did that to get temperature and the country
class WeatherResponse {
#SerializedName("sys")
var sys: Sys? = null
#SerializedName("main")
var main: Main? = null
#SerializedName("weather")
var weather: Weather? = null
}
class Main {
#SerializedName("temp")
var temp: Float = 0.0f
}
and i my main class im using a callback
fun getCurrentData() {
val retrofit = Retrofit.Builder()
.baseUrl(BaseUrl)
.addConverterFactory(GsonConverterFactory.create())
.build()
val service = retrofit.create(WeatherService::class.java)
val call = service.getCurrentWeatherData(lat, lon, AppId)
call.enqueue(object : Callback<WeatherResponse> {
override fun onResponse(call: Call<WeatherResponse>, response: Response<WeatherResponse>) {
if (response.code() == 200) {
val weatherResponse = response.body()!!
var temp = (weatherResponse.main!!.temp - 273).toString().substring(0,3) + " ÂșC"
tmp.text=temp
}
}
override fun onFailure(call: Call<WeatherResponse>, t: Throwable) {
}
})
}

Here is POJO class for your JSON. It now can be parsed easily.
WeatherResponse.kt
data class WeatherResponse(
#SerializedName("cod")
val cod: String? = null,
#SerializedName("count")
val count: Int? = null,
#SerializedName("list")
val list: List<X?>? = null,
#SerializedName("message")
val message: String? = null
) {
data class X(
#SerializedName("clouds")
val clouds: Clouds? = null,
#SerializedName("coord")
val coord: Coord? = null,
#SerializedName("dt")
val dt: Int? = null,
#SerializedName("id")
val id: Int? = null,
#SerializedName("main")
val main: Main? = null,
#SerializedName("name")
val name: String? = null,
#SerializedName("rain")
val rain: Any? = null,
#SerializedName("snow")
val snow: Any? = null,
#SerializedName("sys")
val sys: Sys? = null,
#SerializedName("weather")
val weather: List<Weather?>? = null,
#SerializedName("wind")
val wind: Wind? = null
)
data class Clouds(
#SerializedName("all")
val all: Int?
)
data class Coord(
#SerializedName("lat")
val lat: Double? = null,
#SerializedName("lon")
val lon: Double? = null
)
data class Main(
#SerializedName("humidity")
val humidity: Int? = null,
#SerializedName("pressure")
val pressure: Int? = null,
#SerializedName("temp")
val temp: Double? = null,
#SerializedName("temp_max")
val tempMax: Double? = null,
#SerializedName("temp_min")
val tempMin: Double? = null
)
data class Sys(
#SerializedName("country")
val country: String?
)
data class Weather(
#SerializedName("description")
val description: String? = null,
#SerializedName("icon")
val icon: String? = null,
#SerializedName("id")
val id: Int? = null,
#SerializedName("main")
val main: String? = null
)
data class Wind(
#SerializedName("deg")
val deg: Int? = null,
#SerializedName("gust")
val gust: Int? = null,
#SerializedName("speed")
val speed: Double? = null
)
}
You can get Weather description by using
var description = weatherResponse.list?.get(0)?.weather?.get(0)?.description

Related

Access data json in golang

I have the following JSON document:
{
id: int
transaction_id: string
total: string
line_items: [
{
id: int
name: string
}
]
}
This is my code
type Order struct {
ID int `json:"id"`
TransactionId string `json:"transaction_id"`
Total string `json:"total"`
LineItems []interface{} `json:"line_items"`
}
...
var order Order
json.Unmarshal([]byte(sbody), &order)
for index, a := range order.LineItems {
fmt.Println(a["name"])
}
I got the error:
invalid operation: cannot index a (variable of type interface{})
Should I create an Item struct?
Modify the LineItems field type to []map[string]interface{}.
package main
import (
"encoding/json"
"fmt"
)
func main() {
type Order struct {
ID int `json:"id"`
TransactionId string `json:"transaction_id"`
Total string `json:"total"`
LineItems []map[string]interface{} `json:"line_items"`
}
var order Order
err := json.Unmarshal([]byte(`{
"id": 1,
"transaction_id": "2",
"total": "3",
"line_items": [
{
"id": 2,
"name": "444"
}
]
}`), &order)
if err != nil {
panic(err)
}
for _, a := range order.LineItems {
fmt.Println(a["name"])
}
}

How to mark latest datetime from the list of multiple same day dates

Json list -
[
{
"date": "2022-10-10T09:53:40.835519+05:30",
"value": 10
},
{
"date": "2022-10-13T09:53:40.835519+05:30",
"value": 12
},
{
"date": "2022-10-13T10:53:40.835519+05:30",
"value": 15
},
{
"date": "2022-10-15T10:53:40.835519+05:30",
"value": 20
}
]
in above list if there are multiple dateTimes for same day ( ex. 2022-10-13 )
so how to mark 2022-10-13T10:53 date in object list as isLatestDateForSameDay=true as 2022-10-
13T10:53 is latest compare to 2022-10-13T09:53.
and if there is only one dateTime then it should also marked as isLatestDateForSameDay=true
ex. (2022-10-10T09:53:40.835519+05:30 and 2022-10-15T10:53:40.835519+05:30)
DataListItem class -
class DataListItem {
String date;
int value;
bool isLatestDate;
DataListItem({
required this.date,
required this.value,
this.isLatestDateForSameDay = false,
});
}
Expected list of Objects -
[
DataListItem(date: '2022-10-10T09:53:40.835519+05:30', value: 10, isLatestDateForSameDay: true),
DataListItem(date: '2022-10-13T09:53:40.835519+05:30', value: 12, isLatestDateForSameDay: false),
DataListItem(date: '2022-10-13T10:53:40.835519+05:30', value: 15, isLatestDateForSameDay: true),
DataListItem(date: '2022-10-15T10:53:40.835519+05:30', value: 20, isLatestDateForSameDay: true),
];
Lets assume your json is jsonData with collection package you can get what you want:
var grouped = groupBy(
jsonData,
(Map item) => (item['date'] as String).substring(0, 10),
);
List<DataListItem> result = [];
for (var element in grouped.entries) {
if (element.value.length == 1) {
result.add(DataListItem(
date: element.value.first['date'] as String,
value: element.value.first['value'] as int,
isLatestDate: true));
} else {
var latesItem = findLatestDate(element.value);
element.value.remove(latesItem);
result.add(DataListItem(
date: latesItem['date'] as String,
value: latesItem['value'] as int,
isLatestDate: true));
element.value.forEach((e) => result.add(DataListItem(
date: e['date'] as String,
value: e['value'] as int,
isLatestDate: false)));
}
}
Map<String, dynamic> findLatestDate(List<Map<String, dynamic>> dateList) {
Map<String, dynamic>? result;
for (var element in dateList) {
if (result == null) {
result = element;
} else {
DateTime resultDate =
DateFormat("yyyy-MM-ddThh:mm:ss").parse(result['date'] as String);
DateTime tempDate =
DateFormat("yyyy-MM-ddThh:mm:ss").parse(element['date'] as String);
if (tempDate.isAfter(resultDate)) {
result = element;
}
}
}
return result!;
}
for (var element in result) {
print("result= ${element.date} ${element.value} ${element.isLatestDate}");
// result= 2022-10-10T09:53:40.835519+05:30 10 true
// result= 2022-10-13T10:53:40.835519+05:30 15 true
// result= 2022-10-13T09:53:40.835519+05:30 12 false
// result= 2022-10-15T10:53:40.835519+05:30 20 true
}
also use intl for DateFormat.
Try sorting the list with DateTime.parse()
List<DataListItem> dataListItemlist = [];
list.sort(
(a, b) {
return DateTime.parse(a["date"]).compareTo(DateTime.parse(b["date"]));
},
);
List<String> repeatedDate = [];
for (var i = list.length - 1; i >= 0; i--) {
Map item = list[i];
DateTime date = DateTime.parse(item["date"]);
int day = date.day;
int month = date.month;
int year = date.year;
String formatedDate = "$day-$month-$year";
if (repeatedDate.contains(formatedDate)) {
dataListItemlist.add(
DataListItem(
date: item["date"],
value: item["value"],
isLatestDateForSameDay: false,
),
);
} else {
dataListItemlist.add(
DataListItem(
date: item["date"],
value: item["value"],
isLatestDateForSameDay: true,
),
);
repeatedDate.add(formatedDate);
}
}

Making JSON string to Kotlin object not working

I am trying to send a multipart/formdata object to the backend that contains a Freelancer object and two image files that have to be stored on the server. The saving on disc part works, but saving the JSON string as a freelancer object is not working. I tried converting the string with Jackson objectmapper but I think I am doing something wrong. When I debug the application it crashes at mapper.readValue and goes straight to the catch().
I also tried to work with kotlinx.serializer, but the import just would not work so I switched to Jackson.
The Kotlin controller that takes in the request:
private val imageDirectory = System.getProperty("user.dir") + "/images/"
#PostMapping(consumes = ["multipart/form-data"])
fun saveUser(
#RequestParam("profileImage") profileImage: MultipartFile,
#RequestParam("fileIdImage") fileIdImage: MultipartFile,
#RequestParam("freelancer") freelancer: String,
): ResponseEntity<*> {
return try {
val mapper = ObjectMapper();
val freelancerJson: Freelancer = mapper.readValue(freelancer, Freelancer::class.java)
println(freelancerJson.aboutYou)
makeDirectoryIfNotExist(imageDirectory)
val profileImagePath: Path = Paths.get(imageDirectory, profileImage.originalFilename)
val idImagePath: Path = Paths.get(imageDirectory, fileIdImage.originalFilename)
Files.write(profileImagePath, profileImage.bytes);
Files.write(idImagePath, fileIdImage.bytes);
JsonResponse(HttpStatus.OK, "Saved freelancer} ").createResponseEntity()
} catch (e: Exception) {
JsonResponse(HttpStatus.INTERNAL_SERVER_ERROR, e.message.toString()).createResponseEntity()
}
}
The request from the front end using vue:
The console output of the formdata:
Freelancer model:
#Entity
data class Freelancer(
#Id
val id: Int,
//maps ID to freelancer primary key
#MapsId
#OneToOne(targetEntity = User::class)
#JoinColumn(name = "freelancer_id")
//ignores the freelancer id because it is already mapped to val id
#JsonIgnore
val freelancerId: User,
val firstName: String? = null,
val lastName: String? = null,
val dateOfBirth: Date? = null,
val kvk: String? = null,
val btw: String? = null,
val phone: String? = null,
val street: String? = null,
val zipcode: String? = null,
val city: String? = null,
val housenumber: Int? = 0,
val addition: String? = null,
val nameCardHolder: String? = null,
val iban: String? = null,
val referral: String? = null,
val specialism: String? = null,
val aboutYou: String? = null,
val motivation: String? = null,
val workExperience: Int? = null,
val driverLicense: Boolean? = null,
val ownTransport: Boolean? = null,
val identificationImage: String? = null,
val smallBusinessScheme: Boolean? = false,
val profileImage: String? = null,
val previousWorkedPlatform: String? = null,
val servicesAmount: Int? = null,
val previousWorkedRating: Int? = null,
val foundBy: String? = null,
val privacyStatement: Boolean? = null,
)
I solved it by adding ID to the Json object I was sending to the backend, because my model has a one-to-one relation with another model I had to include it when mapping the Json to the model.

Parsing a List in Flutter / Dart

I've a list that looks like this:
[{id: 1, user_id: 3, challenge_id: 1, created_at: 2019-06-09 06:36:39, image_caption: Enter your image caption here, image_path: https://res.cloudinary.com/dqrmgdpcf/image/upload/v1560062199/zex61jegvqwkevq6qrmd.jpg, image: null, user_upvoted: null, user_downvoted: null, score: 0}, {id: 2, user_id: 2, challenge_id: 1, created_at: 2019-06-12 09:17:07, image_caption: , image_path: https://res.cloudinary.com/dqrmgdpcf/image/upload/v1560331027/dj94sjzufx8gznyxrves.jpg, image: null, user_upvoted: null, user_downvoted: null, score: 0},
{id: 2, user_id: 3, challenge_id: 1, created_at: 2019-06-09 06:36:39, image_caption: Enter your image caption here, image_path: https://res.cloudinary.com/dqrmgdpcf/image/upload/v1560062199/zex61jegvqwkevq6qrmd.jpg, image: null, user_upvoted: null, user_downvoted: null, score: 0}, {id: 2, user_id: 2, challenge_id: 1, created_at: 2019-06-12 09:17:07, image_caption: , image_path: https://res.cloudinary.com/dqrmgdpcf/image/upload/v1560331027/dj94sjzufx8gznyxrves.jpg, image: null, user_upvoted: null, user_downvoted: null, score: 0},
{id: 3, user_id: 3, challenge_id: 1, created_at: 2019-06-09 06:36:39, image_caption: Enter your image caption here, image_path: https://res.cloudinary.com/dqrmgdpcf/image/upload/v1560062199/zex61jegvqwkevq6qrmd.jpg, image: null, user_upvoted: null, user_downvoted: null, score: 0}, {id: 2, user_id: 2, challenge_id: 1, created_at: 2019-06-12 09:17:07, image_caption: , image_path: https://res.cloudinary.com/dqrmgdpcf/image/upload/v1560331027/dj94sjzufx8gznyxrves.jpg, image: null, user_upvoted: null, user_downvoted: null, score: 0}]
So it's basically 3 objects.
The object model looks like this:
class ChallengeUpload {
int id = 0;
int userId = 0;
int challengeId = 0;
String createdAt = "";
String imageCaption = "";
String imagePath = "";
File image;
String userUpvoted = "";
String userDownvoted = "";
int score = 0;
ChallengeUpload();
ChallengeUpload.fromJson(Map<String, dynamic> json) {
id = json['id'];
userId = json['user_id'];
challengeId = json['challenge_id'];
createdAt = json['created_at'];
imageCaption = json['image_caption'];
imagePath = json['image_path'];
userUpvoted = json['user_upvoted'];
userDownvoted = json['user_downvoted'];
score = json['score'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['id'] = this.id;
data['user_id'] = this.userId;
data['challenge_id'] = this.challengeId;
data['created_at'] = this.createdAt;
data['image_caption'] = this.imageCaption;
data['image_path'] = this.imagePath;
data['user_upvoted'] = this.userUpvoted;
data['user_downvoted'] = this.userDownvoted;
data['score'] = this.score;
return data;
}
}
I can't seem to figure out how to parse this list.
If it's just a single object that's being returned by my API I run it through this function:
currentChallenge = Challenge.fromJson(response.data);
How to do something similar but then end up with a UploadedChallengesList instead of a single object?
I've tried:
challengeUploads = json.decode(response.data).map<ChallengeUpload>((dynamic challengeUpload) => ChallengeUpload.fromJson(challengeUpload)).toList();
It'll print:
I/flutter ( 2660): type 'List<dynamic>' is not a subtype of type 'String'
My JSON:
{
id:1,
user_id:3,
challenge_id:1,
created_at:2019-06 -09 06:36:39,
image_caption:Enter your image caption here,
image_path: https://res.cloudinary.com/dqrmgdpcf/image/upload/v1560062199/zex61jegvqwkevq6qrmd.jpg,
image:null,
user_upvoted:null,
user_downvoted:null,
score:0
},
{
id:2,
user_id:2,
challenge_id:1,
created_at:2019-06 -12 09:17:07,
image_caption:,
image_path: https://res.cloudinary.com/dqrmgdpcf/image/upload/v1560331027/dj94sjzufx8gznyxrves.jpg,
image:null,
user_upvoted:null,
user_downvoted:null,
score:0
}
Edit: I found out it had to do with the library I'm using to do API calls. I use Dio and my code ended up looking like this:
Response response = await Dio().get(url,
options: Options(
headers: {"Authorization": accessToken},
responseType: ResponseType.json));
setState(() {
challengeUploads = response.data
.map<ChallengeUpload>((dynamic challengeUpload) =>
ChallengeUpload.fromJson(challengeUpload))
.toList();
});
How about this?
List<Map<String, dynamic>> jsonList = json.decode(response.data.toString()) as List;
List<ChallengeUpload> myList = jsonList.map(
(jsonElement) => ChallengeUpload.fromJson(jsonElement)
).toList();

JSON Parsing using Encodable class

I am trying to parse long response data :
below is the code structure
struct VizLog_UserProfile: Codable {
let data: UserProfileData
let app: App
}
struct UserProfileData: Codable {
let roles: [String]
let users: UserDetails
let member: ProfileMember
enum CodingKeys: String, CodingKey {
case roles
case users
case member
}
}
struct UserDetails: Codable {
let user_id: String
let auth_id: String
let unit_id: String
let company_id: String
let visitor_id: String
let session_token: String
let gender: String
let visitor_type: String
let email: String
let first_name: String
let last_name: String
let mobile: String
let avatar: String
var complex: Complex? = nil
enum CodingKeys: String, CodingKey {
case user_id
case auth_id
case unit_id
case company_id
case visitor_id
case session_token
case gender
case visitor_type
case email
case first_name
case last_name
case mobile
case avatar
case complex = "complex"
}
enum Complex_CodingKeys: String, CodingKey {
case company_id
case unit_id
case complex_name
case setup_steps
case complex_type
case access_type
case pass_type
}
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
user_id = getValUserDetails(key: .user_id, decoder: decoder, container: container)
auth_id = getValUserDetails(key: .auth_id, decoder: decoder, container: container)
unit_id = getValUserDetails(key: .unit_id, decoder: decoder, container: container)
company_id = getValUserDetails(key: .company_id, decoder: decoder, container: container)
visitor_id = getValUserDetails(key: .visitor_id, decoder: decoder, container: container)
session_token = getValUserDetails(key: .session_token, decoder: decoder, container: container)
gender = getValUserDetails(key: .gender, decoder: decoder, container: container)
visitor_type = getValUserDetails(key: .visitor_type, decoder: decoder, container: container)
email = getValUserDetails(key: .email, decoder: decoder, container: container)
first_name = getValUserDetails(key: .first_name, decoder: decoder, container: container)
last_name = getValUserDetails(key: .last_name, decoder: decoder, container: container)
mobile = getValUserDetails(key: .mobile, decoder: decoder, container: container)
avatar = getValUserDetails(key: .avatar, decoder: decoder, container: container)
var tmpComplexUnkeyedContainer = try container.nestedUnkeyedContainer(forKey: .complex)
var tmpComplex : Complex? = nil
let tmpComplexContainer = try tmpComplexUnkeyedContainer.nestedContainer(keyedBy: Complex.CodingKeys.self)
tmpComplex?.company_id = getValComplex(key: .company_id, container: tmpComplexContainer)
tmpComplex?.unit_id = getValComplex(key: .unit_id, container: tmpComplexContainer)
tmpComplex?.complex_name = getValComplex(key: .complex_name, container: tmpComplexContainer)
tmpComplex?.setup_steps = getValComplex(key: .setup_steps, container: tmpComplexContainer)
tmpComplex?.complex_type = getValComplex(key: .complex_type, container: tmpComplexContainer)
tmpComplex?.access_type = getValComplex(key: .access_type, container: tmpComplexContainer)
tmpComplex?.pass_type = getValComplex(key: .pass_type, container: tmpComplexContainer)
complex = tmpComplex
}
}
struct ProfileMember: Codable {
public var units: [String: ProfileUnits]
let parking: [String]
}
struct ProfileUnits: Codable {
let unit_type: String
let building_name: String
let unit_number: String
let building_unit_id: String
enum CodingKeys: String, CodingKey {
case unit_type
case building_name
case unit_number
case building_unit_id
}
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
unit_type = getValProfileMember(key: .unit_type, decoder: decoder, container: container)
building_name = getValProfileMember(key: .building_name, decoder: decoder, container: container)
unit_number = getValProfileMember(key: .unit_number, decoder: decoder, container: container)
building_unit_id = getValProfileMember(key: .building_unit_id, decoder: decoder, container: container)
}
}
struct Subscribed_Applications: Codable {
let app_id: String
let app_name: String
let app_version: String
let logo: String
let end_point: String
let logo_small: String
let logo_medium: String
let logo_large: String
let logo_xsmall: String
}
struct Complex: Codable {
var company_id: String
var unit_id: String
var complex_name: String
var setup_steps: String
var complex_type: String
var access_type: String
var pass_type: String
//let complex_events: [String]
enum CodingKeys: String, CodingKey {
case company_id
case unit_id
case complex_name
case setup_steps
case complex_type
case access_type
case pass_type
//case complex_events
}
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
company_id = getValComplex(key: .company_id, container: container)
unit_id = getValComplex(key: .unit_id, container: container)
complex_name = getValComplex(key: .complex_name, container: container)
setup_steps = getValComplex(key: .setup_steps, container: container)
complex_type = getValComplex(key: .complex_type, container: container)
access_type = getValComplex(key: .access_type, container: container)
pass_type = getValComplex(key: .pass_type, container: container)
//complex_events = getValComplex(key: .complex_events, container: container)
}
}
This is the actual Response which I am trying to parse :
{
"app": {
"version": "v1",
"name": "CHSONE Vizlog",
"time": "2019-02-07 05:31:08"
},
"status_code": 200,
"data": {
"metadata": {
"total": 64899,
"per_page": 10,
"current_page": 1,
"last_page": 6490,
"from": 1,
"to": 10
},
"results": [
{
"log_id": 64899,
"fk_visitor_id": 65103,
"fk_building_unit_id": 97,
"company_id": 7,
"pass_serial_no": null,
"card_no": null,
"number_of_people": 1,
"purpose_of_visit": "Meeting",
"in_gate": "Lobby",
"in_time": "2019-02-06 12:24:31",
"out_time": null,
"out_gate": null,
"in_vehicle_no": null,
"out_vehicle_no": null,
"in_vehicle_color": null,
"out_vehicle_color": null,
"is_handed_over": 0,
"access_type": "card",
"created_at": "2019-02-06 12:24:31",
"updated_at": "2019-02-06 12:24:31",
"created_by": 151,
"updated_by": null,
"visitors": {
"visitor_id": 65103,
"visitor_company_name": null,
"designation": null,
"industry": null,
"used_promotional_channel": null,
"interested_in": null,
"platform": "android",
"iso_code": "IN",
"dial_code": "91",
"company_id": 7,
"first_name": "praduman",
"last_name": null,
"gender": "M",
"mobile": "9080706050",
"email": null,
"image_path": null,
"visitor_type": "guest",
"coming_from": "vashi",
"image_large": null,
"image_medium": null,
"image_small": null
},
"unit": {
"building_unit_id": 97,
"fk_building_id": 1,
"unit_number": "1905",
"floor_no": "19",
"building": {
"building_id": 1,
"building_name": "CyberOne"
}
}
},
{
"log_id": 64898,
"fk_visitor_id": 65054,
"fk_building_unit_id": 97,
"company_id": 7,
"pass_serial_no": null,
"card_no": null,
"number_of_people": 1,
"purpose_of_visit": "Meeting",
"in_gate": "Lobby",
"in_time": "2019-02-06 12:19:06",
"out_time": null,
"out_gate": null,
"in_vehicle_no": null,
"out_vehicle_no": null,
"in_vehicle_color": null,
"out_vehicle_color": null,
"is_handed_over": 0,
"access_type": "card",
"created_at": "2019-02-06 12:19:06",
"updated_at": "2019-02-06 12:19:06",
"created_by": 151,
"updated_by": null,
"visitors": {
"visitor_id": 65054,
"visitor_company_name": null,
"designation": null,
"industry": null,
"used_promotional_channel": null,
"interested_in": null,
"platform": null,
"iso_code": "IN",
"dial_code": "91",
"company_id": 7,
"first_name": "Amit",
"last_name": "Dhawale",
"gender": "M",
"mobile": "8055954796",
"email": null,
"image_path": "http://test.prosimvizlog.s3-ap-southeast-1.amazonaws.com/7/visitor/visitor_65054.jpg",
"visitor_type": "guest",
"coming_from": "sanpada",
"image_large": "http://test.prosimvizlog.s3-ap-southeast-1.amazonaws.com/7/visitor/visitor_65054_large.jpg",
"image_medium": "http://test.prosimvizlog.s3-ap-southeast-1.amazonaws.com/7/visitor/visitor_65054_medium.jpg",
"image_small": "http://test.prosimvizlog.s3-ap-southeast-1.amazonaws.com/7/visitor/visitor_65054_small.jpg"
},
"unit": {
"building_unit_id": 97,
"fk_building_id": 1,
"unit_number": "1905",
"floor_no": "19",
"building": {
"building_id": 1,
"building_name": "CyberOne"
}
}
},
{
"log_id": 64897,
"fk_visitor_id": 57222,
"fk_building_unit_id": 24,
"company_id": 7,
"pass_serial_no": null,
"card_no": null,
"number_of_people": 1,
"purpose_of_visit": "Meeting",
"in_gate": "Lobby",
"in_time": "2019-02-06 12:08:08",
"out_time": null,
"out_gate": null,
"in_vehicle_no": null,
"out_vehicle_no": null,
"in_vehicle_color": null,
"out_vehicle_color": null,
"is_handed_over": 0,
"access_type": "card",
"created_at": "2019-02-06 12:08:08",
"updated_at": "2019-02-06 12:08:08",
"created_by": 151,
"updated_by": null,
"visitors": {
"visitor_id": 57222,
"visitor_company_name": null,
"designation": null,
"industry": null,
"used_promotional_channel": null,
"interested_in": null,
"platform": null,
"iso_code": "IN",
"dial_code": "91",
"company_id": 7,
"first_name": "Praduman",
"last_name": "Patil",
"gender": "M",
"mobile": "9420256819",
"email": null,
"image_path": null,
"visitor_type": "guest",
"coming_from": "Future Scape interview",
"image_large": null,
"image_medium": null,
"image_small": null
},
"unit": {
"building_unit_id": 24,
"fk_building_id": 1,
"unit_number": "1001",
"floor_no": "10",
"building": {
"building_id": 1,
"building_name": "CyberOne"
}
}
},
{
"log_id": 64896,
"fk_visitor_id": 65054,
"fk_building_unit_id": 96,
"company_id": 7,
"pass_serial_no": null,
"card_no": null,
"number_of_people": 1,
"purpose_of_visit": "Meeting",
"in_gate": "Lobby",
"in_time": "2019-01-31 12:34:18",
"out_time": "2019-01-31 12:40:30",
"out_gate": "Lobby",
"in_vehicle_no": null,
"out_vehicle_no": null,
"in_vehicle_color": null,
"out_vehicle_color": null,
"is_handed_over": 1,
"access_type": "card",
"created_at": "2019-01-31 12:34:18",
"updated_at": "2019-01-31 12:40:30",
"created_by": 152,
"updated_by": 151,
"visitors": {
"visitor_id": 65054,
"visitor_company_name": null,
"designation": null,
"industry": null,
"used_promotional_channel": null,
"interested_in": null,
"platform": null,
"iso_code": "IN",
"dial_code": "91",
"company_id": 7,
"first_name": "Amit",
"last_name": "Dhawale",
"gender": "M",
"mobile": "8055954796",
"email": null,
"image_path": "http://test.prosimvizlog.s3-ap-southeast-1.amazonaws.com/7/visitor/visitor_65054.jpg",
"visitor_type": "guest",
"coming_from": "sanpada",
"image_large": "http://test.prosimvizlog.s3-ap-southeast-1.amazonaws.com/7/visitor/visitor_65054_large.jpg",
"image_medium": "http://test.prosimvizlog.s3-ap-southeast-1.amazonaws.com/7/visitor/visitor_65054_medium.jpg",
"image_small": "http://test.prosimvizlog.s3-ap-southeast-1.amazonaws.com/7/visitor/visitor_65054_small.jpg"
},
"unit": {
"building_unit_id": 96,
"fk_building_id": 1,
"unit_number": "1904",
"floor_no": "19",
"building": {
"building_id": 1,
"building_name": "CyberOne"
}
}
},
{
"log_id": 64895,
"fk_visitor_id": 65054,
"fk_building_unit_id": 96,
"company_id": 7,
"pass_serial_no": null,
"card_no": null,
"number_of_people": 1,
"purpose_of_visit": "Meeting",
"in_gate": "Lobby",
"in_time": "2019-01-31 11:35:09",
"out_time": null,
"out_gate": null,
"in_vehicle_no": null,
"out_vehicle_no": null,
"in_vehicle_color": null,
"out_vehicle_color": null,
"is_handed_over": 0,
"access_type": "card",
"created_at": "2019-01-31 11:35:09",
"updated_at": "2019-01-31 11:35:09",
"created_by": 151,
"updated_by": null,
"visitors": {
"visitor_id": 65054,
"visitor_company_name": null,
"designation": null,
"industry": null,
"used_promotional_channel": null,
"interested_in": null,
"platform": null,
"iso_code": "IN",
"dial_code": "91",
"company_id": 7,
"first_name": "Amit",
"last_name": "Dhawale",
"gender": "M",
"mobile": "8055954796",
"email": null,
"image_path": "http://test.prosimvizlog.s3-ap-southeast-1.amazonaws.com/7/visitor/visitor_65054.jpg",
"visitor_type": "guest",
"coming_from": "sanpada",
"image_large": "http://test.prosimvizlog.s3-ap-southeast-1.amazonaws.com/7/visitor/visitor_65054_large.jpg",
"image_medium": "http://test.prosimvizlog.s3-ap-southeast-1.amazonaws.com/7/visitor/visitor_65054_medium.jpg",
"image_small": "http://test.prosimvizlog.s3-ap-southeast-1.amazonaws.com/7/visitor/visitor_65054_small.jpg"
},
"unit": {
"building_unit_id": 96,
"fk_building_id": 1,
"unit_number": "1904",
"floor_no": "19",
"building": {
"building_id": 1,
"building_name": "CyberOne"
}
}
},
{
"log_id": 64894,
"fk_visitor_id": 65102,
"fk_building_unit_id": 97,
"company_id": 7,
"pass_serial_no": null,
"card_no": null,
"number_of_people": 1,
"purpose_of_visit": "Meeting",
"in_gate": "Lobby",
"in_time": "2019-01-28 14:01:24",
"out_time": null,
"out_gate": null,
"in_vehicle_no": null,
"out_vehicle_no": null,
"in_vehicle_color": null,
"out_vehicle_color": null,
"is_handed_over": 0,
"access_type": "card",
"created_at": "2019-01-28 14:01:24",
"updated_at": "2019-01-28 14:01:24",
"created_by": 151,
"updated_by": null,
"visitors": {
"visitor_id": 65102,
"visitor_company_name": null,
"designation": null,
"industry": null,
"used_promotional_channel": null,
"interested_in": null,
"platform": "android",
"iso_code": "IN",
"dial_code": "91",
"company_id": 7,
"first_name": "hzhd",
"last_name": null,
"gender": "M",
"mobile": "8055678855",
"email": null,
"image_path": null,
"visitor_type": "guest",
"coming_from": "vashi",
"image_large": null,
"image_medium": null,
"image_small": null
},
"unit": {
"building_unit_id": 97,
"fk_building_id": 1,
"unit_number": "1905",
"floor_no": "19",
"building": {
"building_id": 1,
"building_name": "CyberOne"
}
}
},
{
"log_id": 64893,
"fk_visitor_id": 65101,
"fk_building_unit_id": 96,
"company_id": 7,
"pass_serial_no": null,
"card_no": null,
"number_of_people": 1,
"purpose_of_visit": "Meeting",
"in_gate": "Lobby",
"in_time": "2019-01-28 14:00:40",
"out_time": null,
"out_gate": null,
"in_vehicle_no": null,
"out_vehicle_no": null,
"in_vehicle_color": null,
"out_vehicle_color": null,
"is_handed_over": 0,
"access_type": "card",
"created_at": "2019-01-28 14:00:40",
"updated_at": "2019-01-28 14:00:40",
"created_by": 151,
"updated_by": null,
"visitors": {
"visitor_id": 65101,
"visitor_company_name": null,
"designation": null,
"industry": null,
"used_promotional_channel": null,
"interested_in": null,
"platform": "android",
"iso_code": "IN",
"dial_code": "91",
"company_id": 7,
"first_name": "test",
"last_name": "sync",
"gender": "M",
"mobile": "8056799285",
"email": null,
"image_path": null,
"visitor_type": "guest",
"coming_from": "vashi",
"image_large": null,
"image_medium": null,
"image_small": null
},
"unit": {
"building_unit_id": 96,
"fk_building_id": 1,
"unit_number": "1904",
"floor_no": "19",
"building": {
"building_id": 1,
"building_name": "CyberOne"
}
}
},
{
"log_id": 64892,
"fk_visitor_id": 47,
"fk_building_unit_id": 97,
"company_id": 7,
"pass_serial_no": null,
"card_no": null,
"number_of_people": 1,
"purpose_of_visit": "Meeting",
"in_gate": "Lobby",
"in_time": "2019-01-28 13:47:42",
"out_time": null,
"out_gate": null,
"in_vehicle_no": null,
"out_vehicle_no": null,
"in_vehicle_color": null,
"out_vehicle_color": null,
"is_handed_over": 0,
"access_type": "card",
"created_at": "2019-01-28 13:47:42",
"updated_at": "2019-01-28 13:47:42",
"created_by": 151,
"updated_by": null,
"visitors": {
"visitor_id": 47,
"visitor_company_name": null,
"designation": null,
"industry": null,
"used_promotional_channel": null,
"interested_in": null,
"platform": null,
"iso_code": "IN",
"dial_code": "91",
"company_id": 7,
"first_name": "KOTESHWAR",
"last_name": "DEVELOPERS",
"gender": "",
"mobile": "9819328361",
"email": "krishnaenterprises400#gmail.com",
"image_path": "http://test.prosimvizlog.s3-ap-southeast-1.amazonaws.com/7/visitor/visitor_47.jpg",
"visitor_type": "member",
"coming_from": null,
"image_large": "http://test.prosimvizlog.s3-ap-southeast-1.amazonaws.com/7/visitor/visitor_47_large.jpg",
"image_medium": "http://test.prosimvizlog.s3-ap-southeast-1.amazonaws.com/7/visitor/visitor_47_medium.jpg",
"image_small": "http://test.prosimvizlog.s3-ap-southeast-1.amazonaws.com/7/visitor/visitor_47_small.jpg"
},
"unit": {
"building_unit_id": 97,
"fk_building_id": 1,
"unit_number": "1905",
"floor_no": "19",
"building": {
"building_id": 1,
"building_name": "CyberOne"
}
}
},
{
"log_id": 64891,
"fk_visitor_id": 65100,
"fk_building_unit_id": 96,
"company_id": 7,
"pass_serial_no": null,
"card_no": "V013",
"number_of_people": 1,
"purpose_of_visit": "Meeting",
"in_gate": "Lobby",
"in_time": "2019-01-22 05:00:43",
"out_time": null,
"out_gate": null,
"in_vehicle_no": null,
"out_vehicle_no": null,
"in_vehicle_color": null,
"out_vehicle_color": null,
"is_handed_over": 0,
"access_type": "card",
"created_at": "2019-01-22 05:00:43",
"updated_at": "2019-01-22 05:00:45",
"created_by": 151,
"updated_by": null,
"visitors": {
"visitor_id": 65100,
"visitor_company_name": null,
"designation": null,
"industry": null,
"used_promotional_channel": null,
"interested_in": null,
"platform": "android",
"iso_code": "IN",
"dial_code": "91",
"company_id": 7,
"first_name": "test",
"last_name": null,
"gender": "M",
"mobile": "8097679764",
"email": null,
"image_path": null,
"visitor_type": "guest",
"coming_from": "vashi",
"image_large": null,
"image_medium": null,
"image_small": null
},
"unit": {
"building_unit_id": 96,
"fk_building_id": 1,
"unit_number": "1904",
"floor_no": "19",
"building": {
"building_id": 1,
"building_name": "CyberOne"
}
}
},
{
"log_id": 64890,
"fk_visitor_id": 65099,
"fk_building_unit_id": 96,
"company_id": 7,
"pass_serial_no": null,
"card_no": null,
"number_of_people": 1,
"purpose_of_visit": "nnxnx",
"in_gate": "Lobby",
"in_time": "2019-01-21 13:20:07",
"out_time": null,
"out_gate": null,
"in_vehicle_no": null,
"out_vehicle_no": null,
"in_vehicle_color": null,
"out_vehicle_color": null,
"is_handed_over": 0,
"access_type": "card",
"created_at": "2019-01-21 13:20:07",
"updated_at": "2019-01-21 13:20:07",
"created_by": 151,
"updated_by": null,
"visitors": {
"visitor_id": 65099,
"visitor_company_name": null,
"designation": null,
"industry": null,
"used_promotional_channel": null,
"interested_in": null,
"platform": "android",
"iso_code": "IN",
"dial_code": "91",
"company_id": 7,
"first_name": "jznznz",
"last_name": null,
"gender": "M",
"mobile": "9767967797",
"email": null,
"image_path": null,
"visitor_type": "guest",
"coming_from": "hzbzs",
"image_large": null,
"image_medium": null,
"image_small": null
},
"unit": {
"building_unit_id": 96,
"fk_building_id": 1,
"unit_number": "1904",
"floor_no": "19",
"building": {
"building_id": 1,
"building_name": "CyberOne"
}
}
}
]
}
}
It returns with the error :
Error info: typeMismatch(Swift.Array, Swift.DecodingError.Context(codingPath: [CodingKeys(stringValue: "data", intValue: nil), CodingKeys(stringValue: "users", intValue: nil)], debugDescription: "Expected to decode Array but found a dictionary instead.", underlyingError: nil))
Below is the exact line where it causes the error :
var tmpComplexUnkeyedContainer = try container.nestedUnkeyedContainer(forKey: .complex)
Can someone suggest me reason.
Thank you.
Codable structures created by you are not parsable from your given response.
Please try below structure.
// To parse the JSON, add this file to your project and do:
// var respData = try? newJSONDecoder().decode(RespData.self, from: jsonData)
import Foundation
class RespData: Codable {
var app: App?
var statusCode: Int?
var data: DataClass?
enum CodingKeys: String, CodingKey {
case app = "app"
case statusCode = "status_code"
case data = "data"
}
init(app: App?, statusCode: Int?, data: DataClass?) {
self.app = app
self.statusCode = statusCode
self.data = data
}
}
class App: Codable {
var version: String?
var name: String?
var time: String?
enum CodingKeys: String, CodingKey {
case version = "version"
case name = "name"
case time = "time"
}
init(version: String?, name: String?, time: String?) {
self.version = version
self.name = name
self.time = time
}
}
class DataClass: Codable {
var metadata: Metadata?
var results: [Result]?
enum CodingKeys: String, CodingKey {
case metadata = "metadata"
case results = "results"
}
init(metadata: Metadata?, results: [Result]?) {
self.metadata = metadata
self.results = results
}
}
class Metadata: Codable {
var total: Int?
var perPage: Int?
var currentPage: Int?
var lastPage: Int?
var from: Int?
var to: Int?
enum CodingKeys: String, CodingKey {
case total = "total"
case perPage = "per_page"
case currentPage = "current_page"
case lastPage = "last_page"
case from = "from"
case to = "to"
}
init(total: Int?, perPage: Int?, currentPage: Int?, lastPage: Int?, from: Int?, to: Int?) {
self.total = total
self.perPage = perPage
self.currentPage = currentPage
self.lastPage = lastPage
self.from = from
self.to = to
}
}
class Result: Codable {
var logID: Int?
var fkVisitorID: Int?
var fkBuildingUnitID: Int?
var companyID: Int?
var passSerialNo: JSONNull?
var cardNo: String?
var numberOfPeople: Int?
var purposeOfVisit: PurposeOfVisit?
var inGate: Gate?
var inTime: String?
var outTime: String?
var outGate: Gate?
var inVehicleNo: JSONNull?
var outVehicleNo: JSONNull?
var inVehicleColor: JSONNull?
var outVehicleColor: JSONNull?
var isHandedOver: Int?
var accessType: AccessType?
var createdAt: String?
var updatedAt: String?
var createdBy: Int?
var updatedBy: Int?
var visitors: Visitors?
var unit: Unit?
enum CodingKeys: String, CodingKey {
case logID = "log_id"
case fkVisitorID = "fk_visitor_id"
case fkBuildingUnitID = "fk_building_unit_id"
case companyID = "company_id"
case passSerialNo = "pass_serial_no"
case cardNo = "card_no"
case numberOfPeople = "number_of_people"
case purposeOfVisit = "purpose_of_visit"
case inGate = "in_gate"
case inTime = "in_time"
case outTime = "out_time"
case outGate = "out_gate"
case inVehicleNo = "in_vehicle_no"
case outVehicleNo = "out_vehicle_no"
case inVehicleColor = "in_vehicle_color"
case outVehicleColor = "out_vehicle_color"
case isHandedOver = "is_handed_over"
case accessType = "access_type"
case createdAt = "created_at"
case updatedAt = "updated_at"
case createdBy = "created_by"
case updatedBy = "updated_by"
case visitors = "visitors"
case unit = "unit"
}
init(logID: Int?, fkVisitorID: Int?, fkBuildingUnitID: Int?, companyID: Int?, passSerialNo: JSONNull?, cardNo: String?, numberOfPeople: Int?, purposeOfVisit: PurposeOfVisit?, inGate: Gate?, inTime: String?, outTime: String?, outGate: Gate?, inVehicleNo: JSONNull?, outVehicleNo: JSONNull?, inVehicleColor: JSONNull?, outVehicleColor: JSONNull?, isHandedOver: Int?, accessType: AccessType?, createdAt: String?, updatedAt: String?, createdBy: Int?, updatedBy: Int?, visitors: Visitors?, unit: Unit?) {
self.logID = logID
self.fkVisitorID = fkVisitorID
self.fkBuildingUnitID = fkBuildingUnitID
self.companyID = companyID
self.passSerialNo = passSerialNo
self.cardNo = cardNo
self.numberOfPeople = numberOfPeople
self.purposeOfVisit = purposeOfVisit
self.inGate = inGate
self.inTime = inTime
self.outTime = outTime
self.outGate = outGate
self.inVehicleNo = inVehicleNo
self.outVehicleNo = outVehicleNo
self.inVehicleColor = inVehicleColor
self.outVehicleColor = outVehicleColor
self.isHandedOver = isHandedOver
self.accessType = accessType
self.createdAt = createdAt
self.updatedAt = updatedAt
self.createdBy = createdBy
self.updatedBy = updatedBy
self.visitors = visitors
self.unit = unit
}
}
enum AccessType: String, Codable {
case card = "card"
}
enum Gate: String, Codable {
case lobby = "Lobby"
}
enum PurposeOfVisit: String, Codable {
case meeting = "Meeting"
case nnxnx = "nnxnx"
}
class Unit: Codable {
var buildingUnitID: Int?
var fkBuildingID: Int?
var unitNumber: String?
var floorNo: String?
var building: Building?
enum CodingKeys: String, CodingKey {
case buildingUnitID = "building_unit_id"
case fkBuildingID = "fk_building_id"
case unitNumber = "unit_number"
case floorNo = "floor_no"
case building = "building"
}
init(buildingUnitID: Int?, fkBuildingID: Int?, unitNumber: String?, floorNo: String?, building: Building?) {
self.buildingUnitID = buildingUnitID
self.fkBuildingID = fkBuildingID
self.unitNumber = unitNumber
self.floorNo = floorNo
self.building = building
}
}
class Building: Codable {
var buildingID: Int?
var buildingName: BuildingName?
enum CodingKeys: String, CodingKey {
case buildingID = "building_id"
case buildingName = "building_name"
}
init(buildingID: Int?, buildingName: BuildingName?) {
self.buildingID = buildingID
self.buildingName = buildingName
}
}
enum BuildingName: String, Codable {
case cyberOne = "CyberOne"
}
class Visitors: Codable {
var visitorID: Int?
var visitorCompanyName: JSONNull?
var designation: JSONNull?
var industry: JSONNull?
var usedPromotionalChannel: JSONNull?
var interestedIn: JSONNull?
var platform: String?
var isoCode: ISOCode?
var dialCode: String?
var companyID: Int?
var firstName: String?
var lastName: String?
var gender: Gender?
var mobile: String?
var email: String?
var imagePath: String?
var visitorType: VisitorType?
var comingFrom: String?
var imageLarge: String?
var imageMedium: String?
var imageSmall: String?
enum CodingKeys: String, CodingKey {
case visitorID = "visitor_id"
case visitorCompanyName = "visitor_company_name"
case designation = "designation"
case industry = "industry"
case usedPromotionalChannel = "used_promotional_channel"
case interestedIn = "interested_in"
case platform = "platform"
case isoCode = "iso_code"
case dialCode = "dial_code"
case companyID = "company_id"
case firstName = "first_name"
case lastName = "last_name"
case gender = "gender"
case mobile = "mobile"
case email = "email"
case imagePath = "image_path"
case visitorType = "visitor_type"
case comingFrom = "coming_from"
case imageLarge = "image_large"
case imageMedium = "image_medium"
case imageSmall = "image_small"
}
init(visitorID: Int?, visitorCompanyName: JSONNull?, designation: JSONNull?, industry: JSONNull?, usedPromotionalChannel: JSONNull?, interestedIn: JSONNull?, platform: String?, isoCode: ISOCode?, dialCode: String?, companyID: Int?, firstName: String?, lastName: String?, gender: Gender?, mobile: String?, email: String?, imagePath: String?, visitorType: VisitorType?, comingFrom: String?, imageLarge: String?, imageMedium: String?, imageSmall: String?) {
self.visitorID = visitorID
self.visitorCompanyName = visitorCompanyName
self.designation = designation
self.industry = industry
self.usedPromotionalChannel = usedPromotionalChannel
self.interestedIn = interestedIn
self.platform = platform
self.isoCode = isoCode
self.dialCode = dialCode
self.companyID = companyID
self.firstName = firstName
self.lastName = lastName
self.gender = gender
self.mobile = mobile
self.email = email
self.imagePath = imagePath
self.visitorType = visitorType
self.comingFrom = comingFrom
self.imageLarge = imageLarge
self.imageMedium = imageMedium
self.imageSmall = imageSmall
}
}
enum Gender: String, Codable {
case empty = ""
case m = "M"
}
enum ISOCode: String, Codable {
case isoCodeIN = "IN"
}
enum VisitorType: String, Codable {
case guest = "guest"
case member = "member"
}
// MARK: Encode/decode helpers
class JSONNull: Codable, Hashable {
public static func == (lhs: JSONNull, rhs: JSONNull) -> Bool {
return true
}
public var hashValue: Int {
return 0
}
public init() {}
public required init(from decoder: Decoder) throws {
var container = try decoder.singleValueContainer()
if !container.decodeNil() {
throw DecodingError.typeMismatch(JSONNull.self, DecodingError.Context(codingPath: decoder.codingPath, debugDescription: "Wrong type for JSONNull"))
}
}
public func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()
try container.encodeNil()
}
}