0

I have looked at a couple of others SO questions such as this but my problem isn't compatible.

I have an List<LatLng> called fireHazardsList that I would like to convert into a JSON string. A LatLng object has a float latitude, longitude. I am trying to use JsonConvert.SerializeObject to convert my List into a JSON string but currently my JSON is empty when this code is run.

LatLng is a class found in Xamarin Android.Gms.Maps.Model;

fireHazardsList = new List<LatLng>();
populateHazardList();
string coordinates = JsonConvert.SerializeObject(fireHazardsList, Formatting.Indented); 

I am surely missing a step, but I am new to JSON and unsure what I am missing. Currently the JSON string I am being returned is like this:

[{},{},{}]

Here is a screenshot showing that my list is being populated just before the SerializeObject() function is called.

enter image description here

14
  • can you add class properties of LatLng class Commented Aug 2, 2017 at 5:18
  • What does fireHazardsList.Any() return after populateHazardList? If true you're going to need to supply the code for LatLng. Commented Aug 2, 2017 at 5:18
  • can you please add json string and property of LatLng class ? Commented Aug 2, 2017 at 5:20
  • Sorry everyone I have edited my question explaining where LatLng is from Commented Aug 2, 2017 at 5:21
  • have you declared the class as Serializeable ? Commented Aug 2, 2017 at 5:23

4 Answers 4

3

Most likely the LatLng class does not have get;set; properties, which is why the JSON rendering is empty.

Try this to create the properties on the fly:

string coordinates = JsonConvert.SerializeObject(
    fireHazardsList.Select(ll => new { ll.latitude, ll.longitude }),
    Formatting.Indented);
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I will try this
1

To create correct JSON first you need to prepare appropriate model:

    [DataContract]
    public class LatLang
    {
     [DataMember(Name = "lat")]
      public string Latitude { get; set; }

      [DataMember(Name = "longi")]
      public string Longitude { get; set; }
    }

To be able to use Data attributes you will need to choose some other JSON serializer. For example DataContractJsonSerializer or Json.NET.

 LatLang latlangObj = new LatLang
 {
   Latitude = "something",
   Longitude = "something"
 };


string jsonString = JsonConvert.SerializeObject(
new
{
    jsonString = latlangObj
});

So jsonString variable will be:

  {
 "jsonString ": {
"lat": "something",
"longi": "someting"
 }
}

Comments

0

Your problem is somewhere in populateHazardList(); That isn't populating the data as you expect. The fact that you are seeing this:

[{},{},{}]

...shows that the serialization is happening correctly, but there's no data. You need to figure out why.

2 Comments

Thanks Chris, I have updated my answer to show that my list is definitely being populated
If the list has items, the problem is missing the field Datamember on the original object definition as per alaa_sayegh answer.
-1

The following peace of code correctly working for me:-

 List<LatLng> l = new List<LatLng>() ;
        l.Add(new LatLng { latitude=0.090f, longitude=90.8f});
        string s = JsonConvert.SerializeObject(l);

Below is my LatLng class

public class LatLng
{
    public float latitude { get; set; }
    public float longitude { get; set; }
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.