tags:

views:

237

answers:

5

Hi folks,

i have a list of lat/long objects on my server.

eg.

public class LatitudeLongitude
{
    public float Latitude;
    public float Longitude;
}

simple.

now, can i return a collection of these, in json format .. BUT ... i do not want to list the key, just the values.

This means the normal result would be something like ...

{ { lat: 111, long : 222 }, { lat: 333, long : 444 } }   ..

but i'm hoping for...

{ {111, 222}, {333, 444} ..... }
{ {obj1.Lat, obj1.Long}, {obj2.Lat, obj2.Long} ... etc. ... }

Is this possible? I mean, i can make that string on the server side easily. But is that a correct JSON output format?

Can someone please confirm, etc.

cheers :)

PS. I hardly know any Json, so please don't hesitate to correct my poor examples above.

+6  A: 

Use JSON arrays:

[ [111, 222], [333, 444], ... ]
Ayman Hourieh
should be square brackets on the outside instead of curly brackets shouldn't it?
Tom
Yeah, a copy/paste issue. I corrected it right after posting.
Ayman Hourieh
A: 

The ASP.NET MVC JSON serializer should convert a IList<LatitudeLongitude> to something like this:

[{"Latitude":111,"Longitude":222},{"Latitude":333,"Longitude":444} .. ]

I believe that under the ASP.NET MVC JSON serializer is just a hot-wired .NET core Javascript serializer.

I dont think it's possible from .NET using a serializer. Prob need to render the string manually.

cottsak
+2  A: 

If you need to access it using the keys in JSON, you'll need to specify it (or use a 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;
}
Mehrdad Afshari
Answer awarded because of the included conversion code.
Pure.Krome
A: 

Hi,

Ayman's proposal isn't quite correct JSON syntax. As an object {..} is always composed of fields having a name. So we again would have object with 2 fields of type 'array' that don't have a name -> not valid json for an object.

There is a very good description of how a json string can look like at www.json.org. Looking at this description you'll see an object is not allowed without a name String for a field so this {111,222} is not a valid object in json, but this is valid [111,222] array. This is not a valid object {[111,222]} but this is {array1 : [111,222]}.

I guess what you actually want is most likely an array of arrays like this not encapsulated as an object.

[[111,222],[333,444]].

This approach would give you the choice to add as much arrays of lat/long as you like to the enclosing array and at the same time you can process the inner arrays no matter what size the outer array is with an javascript loop.

Hope that helped.

cheers Michael

marksml
It was a copy/paste issue that I corrected right after posting. Sorry about the confusion.
Ayman Hourieh
yeah saw to late, you corrected it. looks fine now.
marksml
A: 

But is that a correct JSON output format?

You can paste in JSON at jslint.com to validate your JSON

dsas