0

I currently have a JSON file, which looks like the following (I have stripped out a lot of the repetitive values, but the structure is the same).

    [{
"page" : {
    "currentPageNumber" : "page1",
    "pageTextLocation" : "\\Assets\\Stories\\Story 1\\Chapter1\\",
    "decision":[{
        "activeDecision" : true,
        "nextPage" : "Page2",
        },
        {
        "activeDecision" : true,
        "nextPage" : "Page2",
        },
        {
        "activeDecision" : true,
        "nextPage" : "Page2",
        }
    ]
    },
"page" : {
    "currentPageNumber" : "page2",
    "pageTextLocation" : "\\Assets\\Stories\\Story 2\\Chapter2\\",
    "decision":[{
        "activeDecision" : true,
        "nextPage" : "Page3",},
        {
        "activeDecision" : true,
        "nextPage" : "Page3",
        },
        {
        "activeDecision" : true,
        "nextPage" : "Page3",
        },
        {
        "activeDecision" : true,
        "nextPage" : "Page3",
        }]
    }}]

I can confirm that the full text of the JSON file loads properly into a string value. However, when I pass that value into JsonArray.Parse(loadedTextValue), the return value includes only the final array in the JSON file. The output only displays the last object in the array. If there are two "page" objects, it will only parse the second. If there are three "page objects, it will only parse the third.

However, if I change the key value name of "page" to a unique name, the parser detects all code and turns it into a usable array value. The following data loads completely into a JsonArray value.

            [{
"page" : {
    "currentPageNumber" : "page1",
    "pageTextLocation" : "\\Assets\\Stories\\Story 1\\Chapter1\\",
    "decision":[{
        "activeDecision" : true,
        "nextPage" : "Page2",
        },
        {
        "activeDecision" : true,
        "nextPage" : "Page2",
        },
        {
        "activeDecision" : true,
        "nextPage" : "Page2",
        }
    ]
    },
"page2" : {
    "currentPageNumber" : "page2",
    "pageTextLocation" : "\\Assets\\Stories\\Story 2\\Chapter2\\",
    "decision":[{
        "activeDecision" : true,
        "nextPage" : "Page3",},
        {
        "activeDecision" : true,
        "nextPage" : "Page3",
        },
        {
        "activeDecision" : true,
        "nextPage" : "Page3",
        },
        {
        "activeDecision" : true,
        "nextPage" : "Page3",
        }]
    }}]

Summary: To parse the entire loaded text file, so that code and iterate through the values, extracting it.

I'm completely out of ideas as to the cause of this behavior. Any input will be greatly appreciated! Thanks!

RAB
  • 317
  • 3
  • 12
  • You've got braces around your first page object but not around the second one. So the whole thing is probably an object not an array. – Matthew Strawbridge Jan 01 '16 at 21:27
  • [Here's some info about whether duplicate keys are valid in JSON](http://stackoverflow.com/q/21832701/382780). The gist is that it's valid (or at least not invalid) JSON, but libraries don't tend to handle it. – 31eee384 Jan 01 '16 at 22:29
  • Thank you for the link @31eee384. Once I understood the underlying problem with my structure, concerning duplicate keys, your information helped tremendously. – RAB Jan 02 '16 at 01:02

1 Answers1

3

It doesn't work because you have multiple keys with the same value. If you have a look at the documentation of JSONValue you won't see any method that accepts a key.Therefore multiple keys with the same value are not supported. You have to change your JSON format to lets say

[
  {
    "type": "page",
    "currentPageNumber": "page1",
    ...
  },
  {
    "type": "page",
    "currentPageNumber": "page2",
    ...
  },
  ...
]

This way Parse() will return an array of JSON objects. Next step would be selecting the correct objects by checking the object type and if it's a page then process the object as a page like displaying the currentPageNumber.

ViRuSTriNiTy
  • 5,017
  • 2
  • 32
  • 58
  • I really appreciate your answer. If you can believe it, it still took some time to understand what I'd done, even after reading your explanation.. Please allow me to repeat it back to you, to confirm I understand the problem correctly. The original structure had a single object inside of an array. When that single object loaded, it then had multiple keys called "page", which continuously overwrote eachother, resulting in this problem. But now, There are multiple objects, each one with its own string value called "page". Thus, no conflict? – RAB Jan 02 '16 at 00:56
  • @RAB Yes, this would result in a slightly different JSON structure but the main issue of having multiple string values with the same name at object level is fixed. – ViRuSTriNiTy Jan 02 '16 at 01:45