Reputation: 87
I have an Object I use JSON.stringify to create JSON string. Than I save that string into file. Then I read that file. Make JSON.parse and try to use that object again. But it does not work anymore. For example if I use [i] to select element it doesnt select element but just take charset like its a string :(
Can any body help with that?
This is some kind of example but actuall JSON is toooo long:
{"featureCollection":
{"layers":"[
{\"layerDefinition\":
{\"currentVersion\": 10.3,
\"id\": 0,
\"supportsCoordinatesQuantization\": true,
\"advancedQueryCapabilities\":
{
\"supportsPagination\": true,
\"supportsDistinct\": true
},
\"geometryType\":
\"esriGeometryPolygon\", \"minScale\": 0,
\"maxScale\": 0,
\"extent\":
{},
\"drawingInfo\":
{\"renderer\":
{\"type\": \"simple\", \"symbol\":
{\"type\": \"esriSFS\", \"style\": \"esriSFSSolid\", \"color\": [76, 129, 205, 191], \"outline\":
{\"type\": \"esriSLS\", \"style\": \"esriSLSSolid\", \"color\": [0, 0, 0, 255], \"width\": 0.75}
}
},
Upvotes: 2
Views: 482
Reputation: 780879
What's going on is that the layers
property of the featureCollection
property is not an array, it's a JSON encoding of an array. You need to decode it again to process the contents. Assuming json_obj
is the full object, you need to do:
var layers = JSON.parse(json_obj.featureCollection.layers);
Then you can access layers[i].layerDefinition.currentDefinition
.
I don't know why it's done that way -- you may want to fix the code that creates the JSON in the first place, and remove the part that calls JSON.stringify()
when storing into the layers
property.
Upvotes: 4
Reputation: 36511
It looks as though the process that is writing the JSON string to a file is escaping the quotes which is causing the issues when you try to parse. You either need to stop the process from escaping the quotes, or use replace
to strip out the escaped quotes of your JSON string prior to passing it to parse
Upvotes: 0