rcdmk / aspJSON

A fast classic ASP JSON parser and encoder for easy JSON manipulation to work with the new JavaScript MV* libraries and frameworks.
MIT License
204 stars 89 forks source link

Multi-level Nested Array #27

Closed RaviRamDhali closed 7 years ago

RaviRamDhali commented 7 years ago

I have the following string pulling from a data-source

[[[14.88,39.31],[14.88,39.31]]]

I need to convert the string into a json object and add the entire object

dim arrayDB
arrayDB = "[[[14.88,39.31],[14.88,39.31]]]"

Dim geometry
Set geometry = new JSONObject
geometry.Add "type", "Polygon"
geometry.Add "coordinates", arrayDB

I also tried

dim newArray
Set newArray = new jsonArray
newArray.Push arrayDB

Dim mm, outputObj 
Set mm = new JSONObject
Set outputObj  =  newArray

outputObj.write

output is : ["[[[14.88,39.31],[14.88,39.31]]]"]

This example does not work. The output has an extra set of [ and quotes. I think I am missing jsonArray ? Any help

rcdmk commented 7 years ago

Hi.

You can use the parse method to let the tools do the work for you:

Dim geometry, coordinates
Set geometry = new JSONObject
Set coordinates = new JSONObject
coordinates.parse(arrayDB) ' coordinates here will have a "data" property with your parsed jsonArray object

geometry.Add "type", "Polygon"
geometry.Add "coordinates", coordinates("data")

You could also set coordinates to the result of the parse:

'...
Set coordinates = new JSONObject
Set coordinates = coordinates.parse(arrayDB) ' coordinates here will be your parsed jsonArray object

geometry.Add "type", "Polygon"
geometry.Add "coordinates", coordinates

This two solutions would output the following when the serialize() or write() methods are called:

{"type":"Polygon","coordinates":[[[14.88,39.31],[14.88,39.31]]]}
RaviRamDhali commented 7 years ago

@rcdmk Using your example above, the result only produces ONE item from the array

[14.88,39.31]
{"type":"Polygon","coordinates":[14.88,39.31]}
rcdmk commented 7 years ago

Mmmm... I will need to take a look at this.

It must have worked the way I've showed you above.

I'll do some tests to find out what is going wrong.

rcdmk commented 7 years ago

This is a real bug with the code.

I'll fix this as soon as possible.

In the meantime you can overcome this by wrapping the value in an object before parsing:

arrayDB = "{""value"":""[[[14.88,39.31],[14.88,39.31]]]""}"

coordinates.parse(arrayDB)

geometry.Add "coordinates", coordinates("value")
RaviRamDhali commented 7 years ago

@rcdmk The 3.5.1 update broke the following json parsing:

I will try to figure out why it is breaking. I reverted back to 3.5.0 and it is working

[[-117.215253710747,32.7973127662131],[-117.215039134026,32.7973623683555],[-117.21399307251,32.7971278852568],[-117.213585376739,32.7971639596199],[-117.212598323822,32.7973533497862],[-117.212115526199,32.7973804054914],[-117.212147712708,32.7970782829836]]
rcdmk commented 7 years ago

Hum... This is strange...

I've tried a small test and it does work:

test = "[[-117.215253710747,32.7973127662131],[-117.215039134026,32.7973623683555],[-117.21399307251,32.7971278852568],[-117.213585376739,32.7971639596199],[-117.212598323822,32.7973533497862],[-117.212115526199,32.7973804054914],[-117.212147712708,32.7970782829836]]"

set json = new jsonObject

set arr = json.parse(test)

arr.write

Outputs:

[[-117.215253710747,32.7973127662131],[-117.215039134026,32.7973623683555],[-117.21399307251,32.7971278852568],[-117.213585376739,32.7971639596199],[-117.212598323822,32.7973533497862],[-117.212115526199,32.7973804054914],[-117.212147712708,32.7970782829836]]

Can you test this on your setup, please?

If you still have problems, please, post the smallest portion of code to reproduce the error.