Reputation: 151
I have some json format using that i created a class with properties.
Below is my json data format:
[{
coordinates: [-74, 40.7],
values: [22.4, 23.2, 21.5, 20.6,12.3],
},
{
coordinates: [-77, 38.90],
values: [13.4, 18.2, 24.5, 10.6, 16.3],
},
{
coordinates: [-87, 41.88],
values: [39.3, 28.8, 10.4, 20.0, 0],
}]
Below is my class created from json data:
public class MapDataList
{
public List<double> coordinates { get; set; }
public List<double> values { get; set; }
}
and i have to set value for coordinates and values properties like below:
coordinates: [-74, 40.7], values: [22.4, 23.2, 21.5, 20.6,12.3],
So that this value i can use in my map chart.
I have created one method and tried something in below:
public List<MapDataList> CreateMapData()
{
List<MapDataList> lstMapData = new List<MapDataList>();
MapDataList mdl = new MapDataList();
List<double[]> lb1=new List<double[]>();
double[] coordinateItem1 = {-74, 40.7};
lb1.Add(coordinateItem1);
return lstMapData;
}
But question here is i have to set lb1 object data to lstMapData object.
Upvotes: 0
Views: 783
Reputation: 1094
I'm updating this answer, but you should know: it's still NOT clear what you are trying to do.
I made a new fiddle. What this does is:
MapDataList
out of the JSON you provided.I did that last thing because the title of your question seems to indicate you need a list of doubles, while having a list of double-array's.
If this is NOT what you need, please be MORE CLEAR!
Code:
public class Program
{
public static void Main()
{
var json = "[{ coordinates: [-74, 40.7], values: [22.4, 23.2, 21.5, 20.6,12.3], }, { coordinates: [-77, 38.90], values: [13.4, 18.2, 24.5, 10.6, 16.3], }, { coordinates: [-87, 41.88], values: [39.3, 28.8, 10.4, 20.0, 0], }]";
var mapDataLists = JsonConvert.DeserializeObject<List<MapDataList>>(json);
var coordinates = mapDataLists.SelectMany(d => d.coordinates);
var values = mapDataLists.SelectMany(d => d.values);
Console.WriteLine("Coordinates");
foreach(var d in coordinates){
Console.WriteLine(d);
}
Console.WriteLine("Values");
foreach(var d in coordinates){
Console.WriteLine(d);
}
}
}
public class MapDataList
{
public List<double> coordinates { get; set; }
public List<double> values { get; set; }
}
Please see this fiddle and TRY it yourself: https://dotnetfiddle.net/C48r6T
Upvotes: 1
Reputation: 23521
Object Initializer is a helpful notion.
Object initializers let you assign values to any accessible fields or properties of an object at creation time without having to invoke a constructor followed by lines of assignment statements.
Also, the easiest way to convert an array (or any collection) to a list is to use the .ToList()
method from System.Linq;
Code:
public static List<MapDataList> CreateMapData(double[] coordinates, double[] values )
{
MapDataList mdl = new MapDataList
{
Coordinates = coordinates.ToList(),
Values = values.ToList()
};
return new List<MapDataList> { mdl };
}
Note that you could even remove mdl and write
public static List<MapDataList> CreateMapData(double[] coordinates, double[] values )
{
return new List<MapDataList> { new MapDataList
{
Coordinates = coordinates.ToList(),
Values = values.ToList()
}};
}
Upvotes: 0