3

I have a larga data JSON which I want to pass to the backend to be parsed there to become into java objects.

To make this I'm using the JSON.stringify function but inside the JSON there is an array attribute that the JSON.stringify is enclosing between quotes ("), so when Gson find it (the way I'm using at the backend to decode the string into objects), it throws an error because this is not an array inside a JSON string representation, but an string attibute inside a JSON string representation.

This is an example of the string generated with JSON.stringify:

{"id":0, "array": "[{\"id\":0, \"cod\": \"XXX\"}, {\"id\":0, \"cod\": \"XXX\"}]"}

The array attribute cannot be converted by Gson because is not an array.

Can anybody help me with this issue?

Thanks a lot.

Lukas Eder
  • 211,314
  • 129
  • 689
  • 1,509
Ronye Vernaes
  • 2,444
  • 1
  • 17
  • 21

2 Answers2

0

You need to write this code before invoke JSON.stringify

if(window.Prototype) {
    delete Object.prototype.toJSON;
    delete Array.prototype.toJSON;
    delete Hash.prototype.toJSON;
    delete String.prototype.toJSON;
}
lancha90
  • 5,064
  • 2
  • 17
  • 9
0

I'd likely prefer to fix the generated JSON, but if that's not possible or otherwise preferable, it looks like you'll simply need to deserialize part of the JSON twice. This could be accomplished with a custom deserializer as follows.

import java.lang.reflect.Type;
import java.util.Arrays;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;

public class GsonFoo
{
  public static void main(String[] args)
  {
    // With the generated JSON corrected:

    // {"id":42, "array": [{"id":1, "cod": "aaa"}, {"id":2, "cod": "bbb"}]}
    String jsonInput = "{\"id\":42, \"array\": [{\"id\":1, \"cod\": \"aaa\"}, {\"id\":2, \"cod\": \"bbb\"}]}";

    Gson gson = new Gson();

    Bar bar1 = gson.fromJson(jsonInput, Bar.class);
    System.out.println(bar1);
    // Bar: id=42, array=[Thing: id=1, cod=aaa, Thing: id=2, cod=bbb]

    // -------------------------

    // With the funky JSON:

    // {"id":42, "array": "[{\"id\":1, \"cod\": \"aaa\"}, {\"id\":2, \"cod\": \"bbb\"}]"}
    String funkyJsonInput = "{\"id\":42, \"array\": \"[{\\\"id\\\":1, \\\"cod\\\": \\\"aaa\\\"}, {\\\"id\\\":2, \\\"cod\\\": \\\"bbb\\\"}]\"}";

    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.registerTypeAdapter(Thing[].class, new FunkyThingArrayDeserializer());
    gson = gsonBuilder.create();

    Bar bar2 = gson.fromJson(funkyJsonInput, Bar.class);
    System.out.println(bar2);
    // Bar: id=42, array=[Thing: id=1, cod=aaa, Thing: id=2, cod=bbb]
  }
}

class FunkyThingArrayDeserializer implements JsonDeserializer<Thing[]>
{
  @Override
  public Thing[] deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
      throws JsonParseException
  {
    String actualJson = context.deserialize(json, String.class);
    return new Gson().fromJson(actualJson, Thing[].class);
  }
}

class Bar
{
  int id;
  Thing[] array;

  @Override
  public String toString()
  {
    return String.format("Bar: id=%d, array=%s", id, Arrays.toString(array));
  }
}

class Thing
{
  int id;
  String cod;

  @Override
  public String toString()
  {
    return String.format("Thing: id=%d, cod=%s", id, cod);
  }
}
Programmer Bruce
  • 64,977
  • 7
  • 99
  • 97