Can I return an array "number, number" in json format?
I have a list of lat / long objects on my server.
eg.
public class LatitudeLongitude
{
public float Latitude;
public float Longitude;
}
just.
now, can I return a collection of them in json format .. BUT ... I don't want to list the key, just the values.
This means that the normal result would be something like ...
{ { lat: 111, long : 222 }, { lat: 333, long : 444 } } ..
but I hope...
{ {111, 222}, {333, 444} ..... } { {obj1.Lat, obj1.Long}, {obj2.Lat, obj2.Long} ... etc. ... }
Is it possible? I mean, I can easily do this line server side. But is this the correct JSON output format?
Can anyone please confirm etc.
cheers :)
PS. I barely know Json, so please feel free to fix my bad examples above.
a source to share
If you need to access it using keys in JSON, you need to specify it (or use JS to convert arrays to objects on the client side). Otherwise, arrays should work:
var json = [ [123, 456], [234, 567] ];
var convertedJson = [];
for (i = 0; i < json.length; ++i) {
var thisObj = new Object();
thisObj.latitude = json[i][0];
thisObj.longitude = json[i][1];
convertedJson[i] = thisObj;
}
a source to share
Ayman's suggestion is not quite correct JSON syntax. Because the {..} object always consists of fields that have a name. Thus, we will again have an object with two fields of type "array" that have no name → invalid json for the object.
There is a very good description of how a json string might look like www.json.org . Looking at this description, you can see that object is not allowed without a String name for the field, so this {111,222} is not a valid object in json, but it is a valid array [111,222]. This is not a valid object {[111,222]}, but it is {array1: [111,222]}.
I guess what you really want is most likely an array of arrays like this that hasn't been encapsulated as an object.
[[111222], [333444]].
This approach will give you the ability to add as many lat / long arrays as you like in the enclosing array, and at the same time, you can handle the inner arrays regardless of the size of the outer array with a javascript loop.
Hope it helped.
amuses Michael
a source to share
The ASP.NET MVC JSON serializer should convert IList<LatitudeLongitude>
to the following:
[{"Latitude":111,"Longitude":222},{"Latitude":333,"Longitude":444} .. ]
I believe under ASP.NET MVC the JSON serializer is just a Javascript.NET serializer with clear guides.
I don't think it is possible from .NET using a serializer. You probably need to display the string manually.
a source to share