简体   繁体   中英

How can JSON be deserialized in Java when it contains $ref and $id generated by Json.Net C#

I have a JSON string generated using Json.Net C# library with this settings:

JsonConvert.DefaultSettings = () => new JsonSerializerSettings
            {
                Formatting = Newtonsoft.Json.Formatting.Indented,
                ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore,
                PreserveReferencesHandling = PreserveReferencesHandling.All
            };

Note the PreserveReferencesHandling to have references generated in json instead of the same object being copied (duplicated) on each object reference.

How can I deserialize the result in java ? Google Gson library fails to parse.

UPDATE: The error can't help much in my opinion: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 154944 path $.Tables[0].Columns[4].ForeignKeyTable.ForeignKeyChildren 1 .Columns[0].ForeignKeyTable.Indexes

I'll put a small fragment of the json data, note the $id, $ref: JSON with references format

Finally I found a java library that I modified to be conform to JsonNet C# serializer format:

https://github.com/jdereg/json-io

Changed @id, @ref and @items to $id, $ref and $values and the string value in $id and $ref from JsonNet C# parsed to java Long (it is a number even in C# serialized format but it is as string type)

Maybe this can help others as well..

https://github.com/LucaGabi/json-io.git

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM