-2

My class:

public class MyClass { 

    public HashMap<Integer, HashMap<String, PointFMP[] >> matchingPages;

    public static class PointFMP{
        public float x;
        public float y;
    }
}

My Json:

{
    "matchingPages": {
        "1": {
            "Butter": [
                {
                    "x": 16.23,
                    "y": 21.11
                },
                {
                    "x": 18.18,
                    "y": 26.67
                }
            ],
            "Cake": [
                {
                    "x": 13.23,
                    "y": 21.11
                }
            ]
        },
        "2": {
            "Other value": [
                {
                    "x": 41.98,
                    "y": 47.62
                }
            ]
        }
    }
}

Parsing:

Gson gson = new GsonBuilder().create();
MyClass response = gson.fromJson(jsonString, MyClass.class);

My error:

11-29 12:56:47.017: W/System.err(8169): com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 11-29 12:56:47.022: W/System.err(8169): at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:176)

Any idea how to properly parse it?

Malachiasz
  • 7,126
  • 2
  • 35
  • 49
  • 1
    BEGIN_OBJECT but was STRING means, when gson was de-serialiizing your json the matchingPages key "1" is string not an int/integer that why gson says its "1" , "2" keys are the string not the Integer objects. – Asif Bhutto Nov 29 '13 at 13:30
  • I don't think it's that, because changing public HashMap> matchingPages; to public HashMap> matchingPages; doesn't help and I parse in other place with public Map items; following json "items": { "146226": { – Malachiasz Nov 29 '13 at 13:44
  • see i have replied with PointFMP and Butter class, your json format is correct. oh i think you did not see that matchingPages in your json is string. – Asif Bhutto Nov 29 '13 at 13:46
  • Possible duplicate of [Why does Gson fromJson throw a JsonSyntaxException: Expected some type but was some other type?](http://stackoverflow.com/questions/33621808/why-does-gson-fromjson-throw-a-jsonsyntaxexception-expected-some-type-but-was-s) – Sotirios Delimanolis Jun 23 '16 at 16:10

2 Answers2

0

Your input is not valid Json. It should look like this:

{
    "1": {
         "Butter": [
         {
            "x": 16.23,
            "y": 21.11
        },
        {
            "x": 18.18,
            "y": 26.67
        }
        ]
    },
    "2": {
        "Butter": [
        {
            "x": 41.98,
            "y": 47.62
        }
        ]
    }
}
Henry
  • 42,982
  • 7
  • 68
  • 84
0

Your json format is correct you can parse it as below

   public class PointFMP {

    @SerializedName(value="Butter")
    List<Butter> butter;

    @Override
    public String toString() {
        return "PointFMP [butter=" + butter + "]";
    }
}


public class Butter {

    public double x;
    public double y;
    @Override
    public String toString() {
        return "PointFMP [x=" + x + ", y=" + y + "]";
    }
}

Gson gson = new Gson();
Map<String, PointFMP> categoryMap  =
 gson.fromJson(reader, new TypeToken<Map<String, Map<String, PointFMP >>>(){}.getType());

result will be as below

{1=PointFMP [butter=[PointFMP [x=16.23, y=21.11], PointFMP [x=18.18, y=26.67]]], 2=PointFMP [butter=[PointFMP [x=41.98, y=47.62]]]}
Asif Bhutto
  • 3,916
  • 1
  • 24
  • 21
  • I cannot copy paste your code, because my Json is actually bigger than that (but error is in the fragment which I quoted here. when I comment out in MyClass field matchingPages then it parses). So I replaced my declaration of this variable with yours public Map> matchingPages; and I get the same error. Why do you replaced PointFMP[] with PointFMP ? PointFMP is an array... – Malachiasz Nov 29 '13 at 13:51
  • Also @SerializedName(value="Butter") is not allowed, because value Butter can be any other string - it's variable value will be different on other requests. – Malachiasz Nov 29 '13 at 13:54
  • 1
    Actually in json matchingPages start from these braces {} means its an object not an array, thats why i replaced it. one thing after this "1":{ // this is an object "Butter":[ // this is an array { inside Butter array you have an object. – Asif Bhutto Nov 29 '13 at 13:55
  • I see your point. Do you think then that it's possible to parse it if value "Butter" can be any other string? – Malachiasz Nov 29 '13 at 13:59
  • Yes you can do it more well , but you have to write custom class and your custom class implements JsonDeserializer – Asif Bhutto Nov 29 '13 at 14:03
  • any tip how to do that? btw. take a look at updated Json in the question. Perhaps it was not obvious before that it's really Map – Malachiasz Nov 29 '13 at 14:29
  • Go here https://sites.google.com/site/gson/gson-user-guide through custom deserialize , using reader and streaming api you can do – Asif Bhutto Nov 29 '13 at 14:34
  • sure I know this page, but I have no Idea how could I use it. Anyway it seems that I was right. The error was somewhere else. I think somewhere returned json was incorrect, but for the case in my question my solution was good. – Malachiasz Nov 29 '13 at 16:26