0

On rare occasion when receiving the message "Additional text found in JSON string after finishing deserializing object." the first thing i do is fix some JSON structure typo of mine using JSONLint.

However this time the lint says the json is valid and i can't see where the problem lies. (As i don't control the data's source i need to ldeserialize to a generic object that i then traverse. - Is this possibly the error on my part?)

I'm deserializing with Unity specific Newtonsoft.Json (JsonNet-Lite)

object resultData = JsonConvert.DeserializeObject<object>(jsonString);

What am i missing?

JSON String

"{\"statements\":[{\"id\":\"14b6382c-ddb9-44c5-a22c-a133cec50711\",\"actor\":{\"objectType\":\"Agent\",\"mbox_sha1sum\":\"f7f99253cf6ede467c3a5425d05bfcd96e524595\",\"name\":\"My Name\"},\"verb\":{\"id\":\"https://w3id.org/xapi/dod-isd/verbs/completed\",\"display\":{\"en-US\":\"completed\"}},\"result\":{\"success\":true,\"completion\":true,\"duration\":\"PT0.05S\"},\"context\":{\"contextActivities\":{\"grouping\":[{\"id\":\"http://example.com/activities/MyActivity_Grandparent\",\"objectType\":\"Activity\"}],\"parent\":[{\"id\":\"http://example.com/activities/MyActivity_Parent\",\"objectType\":\"Activity\"}]}},\"timestamp\":\"2018-02-23T19:18:34.145Z\",\"stored\":\"2018-02-23T19:18:34.145Z\",\"authority\":{\"objectType\":\"Agent\",\"account\":{\"homePage\":\"http://cloud.scorm.com\",\"name\":\"abcdef-ghijk\"},\"name\":\"Test Provider\"},\"version\":\"1.0.0\",\"object\":{\"id\":\"http://example.com/activities/MyActivity\",\"definition\":{\"extensions\":{\"https://w3id.org/xapi/dod-isd/extensions/interactivity-level\":\"3\",\"https://w3id.org/xapi/dod-isd/extensions/ksa\":\"Attitude\",\"https://w3id.org/xapi/dod-isd/extensions/category\":\"Responding\",\"http://example.com/TerminalObjective\":\"My Activity Objective\"},\"name\":{\"en-US\":\"My Activity Name\"},\"description\":{\"en-US\":\"My Activity Description\"},\"type\":\"http://adlnet.gov/expapi/activities/simulation\"},\"objectType\":\"Activity\"}}],\"more\":\"/tc/Z5R2XATQZW/sandbox/statements?continueToken=e50555fe-0c3d-4663-91c4-7f0ff7df4ccf\"}"

JSON Formatted for readability

{
"statements": [{
    "id": "14b6382c-ddb9-44c5-a22c-a133cec50711",
    "actor": {
        "objectType": "Agent",
        "mbox_sha1sum": "f7f99253cf6ede467c3a5425d05bfcd96e524595",
        "name": "My Name"
    },
    "verb": {
        "id": "https://w3id.org/xapi/dod-isd/verbs/completed",
        "display": {
            "en-US": "completed"
        }
    },
    "result": {
        "success": true,
        "completion": true,
        "duration": "PT0.05S"
    },
    "context": {
        "contextActivities": {
            "grouping": [{
                "id": "http://example.com/activities/MyActivity_Grandparent",
                "objectType": "Activity"
            }],
            "parent": [{
                "id": "http://example.com/activities/MyActivity_Parent",
                "objectType": "Activity"
            }]
        }
    },
    "timestamp": "2018-02-23T19:18:34.145Z",
    "stored": "2018-02-23T19:18:34.145Z",
    "authority": {
        "objectType": "Agent",
        "account": {
            "homePage": "http://cloud.scorm.com",
            "name": "abcdef-ghijk"
        },
        "name": "Test Provider"
    },
    "version": "1.0.0",
    "object": {
        "id": "http://example.com/activities/MyActivity",
        "definition": {
            "extensions": {
                "https://w3id.org/xapi/dod-isd/extensions/interactivity-level": "3",
                "https://w3id.org/xapi/dod-isd/extensions/ksa": "Attitude",
                "https://w3id.org/xapi/dod-isd/extensions/category": "Responding",
                "http://example.com/TerminalObjective": "My Activity Objective"
            },
            "name": {
                "en-US": "My Activity Name"
            },
            "description": {
                "en-US": "My Activity Description"
            },
            "type": "http://adlnet.gov/expapi/activities/simulation"
        },
        "objectType": "Activity"
    }
}],
"more": "/tc/Z5R2XATQZW/sandbox/statements?continueToken=e50555fe-0c3d-4663-91c4-7f0ff7df4ccf"
}
Reahreic
  • 596
  • 2
  • 7
  • 26
  • Your Json is invalid.You cannot de-serialize invalid json. It will not work. This is why you get the error. Paste the json [here](http://json2csharp.com/) and you will see bunch of `invalid_name` variables. A proper json shouldn't have those. You ether have to fix that on the server or use different source to get your json if you can't fix it on the server – Programmer Feb 23 '18 at 19:54
  • @Programmer - it's perfectly valid JSON, upload to https://jsonlint.com/ and you will get no errors. The problem is that some of the JSON property names are invalid c# identifiers. In regular .Net one would deserialize to a `Dictionary` as shown [here](https://stackoverflow.com/a/24536564). OP is using Json.NET on unity3d so I *guess* it should work there also, but I'm not 100% sure since it's actually *JsonNet-Lite*. – dbc Feb 23 '18 at 20:00
  • Can't reproduce on regular .Net, see https://dotnetfiddle.net/mmK03o. Are you sure there aren't some non-printing Unicode characters (such as a BOM) at the beginning or end of your JSON string? – dbc Feb 23 '18 at 20:04
  • @dbc It also validated with the link I posted but still not valid due to the variable names. Try to generate Objects from that json. You will see that some of them have invalid variable names. That's another way to find out if there is something wrong with the json – Programmer Feb 23 '18 at 20:06
  • @Programmer - i see what you mean about invalid names (from a C# perspective) I figured the library i was using to serilize would be able to desearialize a Dictionary, apparently not... Your link is great, i wasn't aware of that site and will add it to my list of resources. – Reahreic Feb 23 '18 at 20:25
  • @dbc - I'll upgrade to JsonNet (not-Lite) as it appears that the -lite only supports serializing dictionaries and not deserilizing them... Will post shortly with the details. – Reahreic Feb 23 '18 at 20:25
  • @Reahreic - is this JsonNet-Lite: https://github.com/SaladLab/Json.Net.Unity3D/releases? The release notes say *JsonNet-Lite: Stripped package which doesn't have JsonLinq, JPath and Bson* which may be your problem since normally `JsonConvert.DeserializeObject(jsonString);` will actually deserialize to a `JToken` - which has allegedly been removed from the build. So upgrading to Json.NET full might well work. – dbc Feb 23 '18 at 20:48

1 Answers1

0

The deserialization method, you chose, is rather for a case, when you are deserializing a well-known Json to an object, which matches the structure. In this case the Json structure does not match the System.Object type structure, you are trying to deserialize to. The library complaints, that there's much more in the Json, than in the System.Object structure.

For parsing any Json object, you could try an example from the Newtonsoft.Json documentation:

string json = @"{
  CPU: 'Intel',
  Drives: [
    'DVD read/writer',
    '500 gigabyte hard drive'
  ]
}";
JObject o = JObject.Parse(json);

You will still need to traverse the deserialized object. Depending on what you want to achieve, it may actually be much better to look for a .Net class that matches the Json structure (or write one).

Hilarion
  • 820
  • 7
  • 21