Reputation: 2899
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.
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);
Upvotes: 0
Views: 270
Reputation: 2807
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"
]
}
Upvotes: 1
Reputation: 48884
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.
Upvotes: 1
Reputation: 10497
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\"]
}
Upvotes: 1