0

I'm trying to parse a JSON object using gson like this:

1st,2nd,3rd,4th

Gson gson=new Gson(); 

Map<String, HashSet<String>> map = new HashMap<String, HashSet<String>>();

map = (Map<String, HashSet<String>>) gson.fromJson(jsonTxt, map.getClass());

This is what the JSON looks like:

"{
  \"Rao\":[\"Q7293658\",\"\",\"Q7293657\",\"Q12953055\",\"Q3531237\",\"Q4178159\",\"Q1138810\",\"Q579515\",\"Q3365064\",\"Q7293664\",\"Q1133815\"],
  \"Hani Durzy\":[\"\"],
  \"Louise\":[\"\",\"Q1660645\",\"Q130413\",\"Q3215140\",\"Q152779\",\"Q233203\",\"Q7871343\",\"Q232402\",\"Q82547\",\"Q286488\",\"Q156723\",\"Q3263649\",\"Q456386\",\"Q233192\",\"Q14714149\",\"Q12125864\",\"Q57669\",\"Q168667\",\"Q141410\",\"Q166028\"],
  \"Reyna\":[\"Q7573462\",\"Q2892895\",\"Q363257\",\"Q151944\",\"Q3740321\",\"Q2857439\",\"Q1453358\",\"Q7319529\",\"Q733716\",\"Q16151941\",\"Q7159448\",\"Q5484172\",\"Q6074271\",\"Q1753185\",\"Q7319532\",\"Q5171205\",\"Q3183869\",\"Q1818527\",\"Q251862\",\"Q3840414\",\"Q5271282\",\"Q5606181\"]
}"

I keep getting the error:

Expected BEGIN_OBJECT but was STRING at line 1 column 2

I don't think it has anything to do with the fact that it begins with a ", as they suggest in this question, since when I removed the first and last quotes I got the error Expected name at line 2 column 3

My full code is here on my github page, it's pretty short.

I'm essentially trying to reverse this procedure. This is a related question.

UPDATE

        File f = new File("/home/matthias/Workbench/SUTD/nytimes_corpus/wdtk-parent/wdtk-examples/JSON_Output/user.json");

        String jsonTxt = null;

        if (f.exists())
        {
            InputStream is = new FileInputStream("/home/matthias/Workbench/SUTD/nytimes_corpus/wdtk-parent/wdtk-examples/JSON_Output/user.json");
            jsonTxt = IOUtils.toString(is);

        }

        Gson json = new Gson();
        Map<String, HashSet<String>> map = new HashMap<String, HashSet<String>>();
        map = (Map<String, HashSet<String>>) json.fromJson(jsonTxt, map.getClass());
        System.out.println(map);
Community
  • 1
  • 1
smatthewenglish
  • 2,595
  • 3
  • 29
  • 64
  • Go through my answer. – OO7 Apr 28 '15 at 05:46
  • I know u got the solution. But can try my version of reading JSON from file. – OO7 Apr 28 '15 at 06:11
  • @OO7 I tried it on my wonky escaped JSON and I got the error `com.google.gson.JsonPrimitive cannot be cast to com.google.gson.JsonObject`, when I tried it on the normal JSON (after I removed the escapes and the outside quotes) it works, but does that just read it in as a string? I want to get it in as `Map>` – smatthewenglish Apr 28 '15 at 06:39
  • @OO7 but the way you read in the file was way smarter than me, I adopted that part of your code. – smatthewenglish Apr 28 '15 at 06:51

3 Answers3

1

As per error you are facing, you need to remove " from your Json string so that gson will parse it correctly.

For ex, for your json string, corrected string is :

{
  \"Rao\":[\"Q7293658\",\"\",\"Q7293657\",\"Q12953055\",\"Q3531237\",\"Q4178159\",\"Q1138810\",\"Q579515\",\"Q3365064\",\"Q7293664\",\"Q1133815\"],
  \"Hani Durzy\":[\"\"],
  \"Louise\":[\"\",\"Q1660645\",\"Q130413\",\"Q3215140\",\"Q152779\",\"Q233203\",\"Q7871343\",\"Q232402\",\"Q82547\",\"Q286488\",\"Q156723\",\"Q3263649\",\"Q456386\",\"Q233192\",\"Q14714149\",\"Q12125864\",\"Q57669\",\"Q168667\",\"Q141410\",\"Q166028\"],
  \"Reyna\":[\"Q7573462\",\"Q2892895\",\"Q363257\",\"Q151944\",\"Q3740321\",\"Q2857439\",\"Q1453358\",\"Q7319529\",\"Q733716\",\"Q16151941\",\"Q7159448\",\"Q5484172\",\"Q6074271\",\"Q1753185\",\"Q7319532\",\"Q5171205\",\"Q3183869\",\"Q1818527\",\"Q251862\",\"Q3840414\",\"Q5271282\",\"Q5606181\"]
}
Vimal Bera
  • 9,887
  • 4
  • 21
  • 47
1

Try this

public class JsonTest {

    private String  jsonTxt = "{\"Rao\":[\"Q7293658\",\"\",\"Q7293657\",\"Q12953055\",\"Q3531237\","
                    + "\"Q4178159\",\"Q1138810\",\"Q579515\",\"Q3365064\",\"Q7293664\",\"Q1133815\"],"
                    + "\"Hani Durzy\":[\"\"],\"Louise\":[\"\",\"Q1660645\",\"Q130413\",\"Q3215140\","
                    + "\"Q152779\",\"Q233203\",\"Q7871343\",\"Q232402\",\"Q82547\",\"Q286488\","
                    + "\"Q156723\",\"Q3263649\",\"Q456386\",\"Q233192\",\"Q14714149\",\"Q12125864\""
                    + ",\"Q57669\",\"Q168667\",\"Q141410\",\"Q166028\"],\"Reyna\":[\"Q7573462\","
                    + "\"Q2892895\",\"Q363257\",\"Q151944\",\"Q3740321\",\"Q2857439\",\"Q1453358\","
                    + "\"Q7319529\",\"Q733716\",\"Q16151941\",\"Q7159448\",\"Q5484172\",\"Q6074271\""
                    + ",\"Q1753185\",\"Q7319532\",\"Q5171205\",\"Q3183869\",\"Q1818527\",\"Q251862\","
                    + "\"Q3840414\",\"Q5271282\",\"Q5606181\"]}";

    @SuppressWarnings("unchecked")
    @Test
    public void testJson() {
        Gson json = new Gson();
        Map<String, HashSet<String>> map = new HashMap<String, HashSet<String>>();
        map = (Map<String, HashSet<String>>) json.fromJson(jsonTxt, map.getClass());
        System.out.println(map);
    }
}

Output :

{
Reyna=[Q7573462, Q2892895, Q363257, Q151944, Q3740321, Q2857439, Q1453358, Q7319529, Q733716, Q16151941, Q7159448, Q5484172, Q6074271, Q1753185, Q7319532, Q5171205, Q3183869, Q1818527, Q251862, Q3840414, Q5271282, Q5606181], 

Rao=[Q7293658, , Q7293657, Q12953055, Q3531237, Q4178159, Q1138810, Q579515, Q3365064, Q7293664, Q1133815], 

Louise=[, Q1660645, Q130413, Q3215140, Q152779, Q233203, Q7871343, Q232402, Q82547, Q286488, Q156723, Q3263649, Q456386, Q233192, Q14714149, Q12125864, Q57669, Q168667, Q141410, Q166028], 

Hani Durzy=[]
}

From the update, I understood that u r trying to read JSON from file. I have my version to read JSON from file. Check it.

private static JsonObject   chartJson;

public static String readChartJsonFromJsonFile(String fileName) {
    try {
        JsonParser jsonParser = new JsonParser();

        // get json as buffer
        BufferedReader br = new BufferedReader(new FileReader(fileName));

        JsonElement element = jsonParser.parse(br);
        if (!(element instanceof JsonNull)) {
            chartJson = (JsonObject) element;
        }
    } catch (FileNotFoundException e) {
        LoggerManager.fatal("Error reading JSON from file.", JsonFileParser.class.getName());
    }
    return chartJson.toString();
}

My sample JSON in File :

{
   "xArray":[
      "1 Jan 2008",
      "1 Jan 2009",
      "1 Jan 2010",
      "1 Jan 2011",
      "1 Jan 2012",
      "1 Jan 2013",
      "1 Jan 2014"
   ],
   "yArray":[
      "38",
      "87",
      "45",
      "25",
      "67",
      "-37",
      "98"
   ]
}
OO7
  • 2,719
  • 1
  • 16
  • 30
  • i tried the code I just posted in the update and still it didn't work. :/ what could be going on? I'm still getting `Expected BEGIN_OBJECT but was STRING at line 1 column 2` – smatthewenglish Apr 28 '15 at 05:54
1

You've escaped all the " characters and wrapped the whole JSON up in quotes as if this were a String constant in your code (perhaps it was there at one point). But your JsonMapFileExample is reading this JSON from a file on disk - there's no reason to escape the quotes in a dedicated JSON file.

Try removing the outer " characters and the \ escapes around each key and value. You can also paste your JSON directly into any number of online JSON validators to double-check. The contents of your file should be valid JSON; what you've pasted is not.

dimo414
  • 42,340
  • 17
  • 131
  • 218
  • yeah. your exactly right. bravo. i don't know why i have all those escape characters or quotes around the outside, i guess now I need to write a short script to remove that crap. i generated the JSON object with [this file](https://github.com/h1395010/database_builder/blob/master/src/main/java/Q/JsonMapFileExample.java), can you tell why it might be that it added in that extra crap? – smatthewenglish Apr 28 '15 at 06:09
  • I don't see anything in your link that would output source-code-escaped text like that. Are you sure that's what generated the botched output? – dimo414 Apr 28 '15 at 06:17
  • Did you perhaps use [`Escaper`](https://code.google.com/p/google-gson/source/browse/trunk/gson/src/main/java/com/google/gson/Escaper.java?r=597) at some point? I haven't tried it, but it looks like this class would return escaped JSON like what you have. – dimo414 Apr 28 '15 at 17:06