2

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}
                                                    }
                                                },
3
  • Can you post the JSON string? or at least a portion of it? Commented May 1, 2015 at 23:48
  • I added in a question Commented May 1, 2015 at 23:59
  • Solution: Only stringify the root of your data structure. Commented May 2, 2015 at 0:08

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! yes this thing work for me. Firstly I tried to replace escape charset but it didnt work. But anyway thank you a lot!
0

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

2 Comments

It's not happening when the JSON is being written to the file. Notice that the first two lines aren't escaped.
Good eye, didn't catch that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.