Json Data Comes Down But, Object Is Null
You need to implement the Place.fromJSON() according to the structure of the JSON you're receiving. Here you can see an example that could help you to achieve that: how do I collect a loop through a list from http in JSON
Edit: From what I see in your Update 2, the JSON seems to be malformed, it needs to have quotation marks on the keys and on the string values. You could use this to try to structure the JSON correctly: JSON Editor Online
I assume the JSON structure you want to have is this one:
{
"places": [
{
"id": 4,
"place_name": "The Oasis",
"phone": "123-123-1234",
"street_address": "456 Fake St",
"place_logo": "https://restaurant.s3.amazonaws.com/restaurant_logo/restaurantLogo_jXon4qm.jpg",
"place_photo": "https://restaurant.s3.amazonaws.com/restaurant_photo/farma.jpg",
"city": "New York",
"state": "New York",
"zip_Code": 12345,
"lat": 40,
"lng": 40,
"latlng": "(40.7770112244898, -74.2110798163265)",
"opening_hours": [],
"ratings": 3
}
]
}
Then, to parse this JSON you could do something like this:
final String jsonExample = '{"places":[{"id":4,"place_name":"The Oasis","phone":"123-123-1234","street_address":"456 Fake St","place_logo":"https://restaurant.s3.amazonaws.com/restaurant_logo/restaurantLogo_jXon4qm.jpg","place_photo":"https://restaurant.s3.amazonaws.com/restaurant_photo/farma.jpg","city":"New York","state":"New York","zip_Code":12345,"lat":40,"lng":40,"latlng":"(40.7770112244898, -74.2110798163265)","opening_hours":[],"ratings":3}]}';
void testExample() {
final responseData = json.decode(jsonExample);
PlaceResults placeResults = PlaceResults.fromJSON(responseData);
print('$placeResults');
}
class PlaceResults {
List<Place> results;
PlaceResults({this.results});
factory PlaceResults.fromJSON(Map<String, dynamic> json) {
List<Place> tempResults = [];
for (int i = 0; i < json['places'].length; i++) {
tempResults.add(Place.fromJSON(json['places'][i]));
}
return PlaceResults(results: tempResults);
}
@override
String toString() {
return results.fold("",(prev, element)=> '$element,$prev');
}
}
class Place {
int id;
String placeName;
String streetAddress;
String city;
String placePhoto;
String state;
num lat;
num lng;
num ratings;
Place({
this.id,
this.streetAddress,
this.city,
this.placePhoto,
this.lat,
this.lng,
this.placeName,
this.state,
this.ratings,
});
factory Place.fromJSON(Map<String, dynamic> json) {
return Place(
id: json['id'],
placeName: json['place_name'],
streetAddress: json['street_address'],
city: json['city'],
placePhoto: json['place_photo'],
state: json['state'],
lat: json['lat'],
lng: json['lng'],
ratings: json['ratings'],
);
}
@override
String toString() {
return 'id:$id,placeName:$placeName,streetAddress:$streetAddress,city:$city,placePhoto:$placePhoto,state:$state,lat:$lat,lng:$lng,ratings:$ratings';
}
}
Notes:
- I'm using the type num instead of double for lat, lng and ratings since in your response example they are ints but I assume they could also be double, so using num will accept both cases
- I changed the name of the properties in the class Place to be the same as the name of the properties of the response, to avoid confusions
- I removed the castings like json['id'] as int, since it's not necessary
- I override the toString() methods in the classes just to print the example
Doing Things Occasionally
Doing Things Occasionally. Like coding shit.
Updated on December 14, 2022Comments
-
Doing Things Occasionally over 1 year
I'm trying to pass the JSON data from my server into a premade object in Flutter but, I get a null value even though I passed the data to a variable and the corresponding class.
Future getPlaces() async{ _isLoading = true; var url = 'http://Place-2212.herokuapp.com/api/customer/Locations/'; http.get(url, headers: { "Content-Type": "application/x-www-form-urlencoded" }).then((http.Response response) { // print(response.body); final responseData = json.decode(response.body); Place place = Place.fromJSON(responseData); print(place.toJson()); return Place; }); }
The output here is this:
{id: null, name: null, address: null, city: null, placePhoto: null, state: null, lat: null, long: null, rating: null}
How do i get the data that pulls down from the json request into my object?
Update 1:
Here is how Place class is implemented:
class Place { int id; String name; String address; String city; String restaurantPhoto; String state; double lat; double long; String rating; Place({ this.id, this.address, this.city, this.restaurantPhoto, this.lat, this.long, this.name, this.state, this.rating, }); factory Place.fromJSON(Map<String, dynamic> json){ return Place( id: json['id'] as int, name: json[‘restaurant_name'] as String, address: json['street_address'] as String, city: json['city'] as String, restaurantPhoto: json['restaurant_photo'] as String, state: json['state'] as String, lat: json['lat'] as double, long: json['lng'] as double, rating: json['rating'] as String, ); } }
Update 2: Here is the JSON response that I want to serialize.
{places: [{id: 4, place_name: The Oasis, phone: 123-123-1234, street_address: 456 Fake St, place_logo: https://restaurant.s3.amazonaws.com/restaurant_logo/restaurantLogo_jXon4qm.jpg, place_photo: https://restaurant.s3.amazonaws.com/restaurant_photo/farma.jpg, city: New York, state: New York, zip_Code: 12345, lat: 40.0, lng: 40.0, latlng: (40.7770112244898, -74.2110798163265), opening_hours: [], ratings: 3.0},
-
Karol Lisiewicz over 4 yearsHow Place.fromJSON method is implemented? What's the structure of the responseData map?
-
Doing Things Occasionally over 4 yearsUpdating code now
-
Doing Things Occasionally over 4 yearsAdded place implementation
-
Pablo Barrera over 4 yearsCould you post the response you are getting? So we can see if the structure of the JSON is according to what you are trying to parse
-
Doing Things Occasionally over 4 yearsUpdating code now.
-
Pablo Barrera over 4 yearsI updated my answer with a full example
-