1

Hello everyone,

I am currently having trouble trying to parse a JSON from a server response. Im the Client/App side programmer, and we have a Server programmer responsible for creating the JSON. So I don't really know that much about JSON and how they generate them.

I am parsing the JSON in Unity(Game Engine), using the built-in JSON Utility. I've been googling around and there might be some different variations of JSON, so I am not sure If I need a 3rd party library that can parse the specific JSON that I have.

Are you familiar with this type of JSON below?

[
  {
    "id": 221,
    "builder_count": 1,
    "units": "[{\"count\": 1, \"level\": 1, \"type_id\": 1, \"unit_id\": \"2359-1\", \"research_time\": 0, \"research_complete\": 1}],[{\"count\": 5, \"level\": 1, \"type_id\": 11, \"unit_id\": \"2359-11\", \"research_time\": 0, \"research_complete\": 1}],[{\"count\": 5, \"level\": 1, \"type_id\": 2, \"unit_id\": \"2359-2\", \"research_time\": 0, \"research_complete\": 1}]"
  }
]

The Units key seems a bit off, they are being treated as a string even though they are really an object.

I have access to the server code, and this is how the Key:Units is being generated. (The whole code block is pretty big, so I just selected this part as an example, please let me know If I need to post the whole function that generated the entire JSON)

    '    (SELECT CAST(GROUP_CONCAT ( ' +
    '       JSON_ARRAY( ' +
    '           JSON_OBJECT( ' +
    '               "unit_id", tu.unique_id, ' +
    '               "type_id", tu.unit_type_id, ' +
    '               "level", tu.level, ' +
    '               "research_time", tu.research_time, ' +
    '               "research_complete", tu.research_complete, ' +
    '               "count", tu.tally ' +
    '               )' +
    '           )' +
    '       ) AS CHAR)' +
    '     FROM api_TurfUnits tu WHERE tu.turf_id = t.id) AS units, '

Our server technology that we use is Node.js and AWS Aurora (MySQL).

Basically I'm looking for a library that might be able to parse the type of JSON properly (the Key: Units)

I have used the website https://jsonformatter.curiousconcept.com/ and tried all the different JSON standard, but none of them can't seem to parse it.

Albert
  • 151
  • 2
  • 8
  • `they are being treated as a string even though they are really not.` - no, they really are – Jaromanda X Dec 04 '17 at 09:42
  • `this type of JSON` - what? JSON that doesn't parse? that's called invalid JSON, and it's very common in questions on stack overflow – Jaromanda X Dec 04 '17 at 09:47
  • what I meant was, for example the 'buildings' key in the JSON example is being treated as a string when I parse it. But the string value is really an object, and the parser can't seem to recognize that. Could this possibly be an issue with how the JSON is being generated? – Albert Dec 04 '17 at 09:52
  • json parsing isn't recursive ... however, the JSON you've presented does not parse anyway - but if it did, it looks like `"buildings"` etc are actually JSON strings, stringified in the containing object ... whatever is producing that "JSON" is doing it wrong, or not like how you expected – Jaromanda X Dec 04 '17 at 09:54
  • if I assign that OBJECT in your question to x, then, `typeof x[0].buildings === "string"` ... and JSON.parse(x[0].buildings) produces an array ... so, as I suspected, buildings is the result of JSON.stringify (or equivalent) which is then added to an object an JSON.stringify'd again ... you need to fix the code that creates the JSON – Jaromanda X Dec 04 '17 at 09:59
  • @Kaddath Thank you for the explanation, I've just tried what you said, and it worked. Though it looked like there are some formatting error on the Units and Abilities keys (looks like an invalid array). But it worked on the Buildings key. I now know what really is happening. If you like to post your explanation as an answer, I can now mark it and close the case. Thanks again guys. – Albert Dec 04 '17 at 10:51
  • You can't do with the built-tin Json API. You can de-serialize to `object` with `SimpleJSON` or `Newtonsoft.Json` mentioned and linked in the duplicated. Although, don't go this route. Simply name your jsons. Don't make them `object`s. – Programmer Dec 04 '17 at 13:28

1 Answers1

1

JSON.parse will take care of most of your needs.

If you're using the request library there is an option to pre-parse it into json.

danieltan95
  • 810
  • 7
  • 14