I get a String in the following format:
String buffer = "[{\"field1\": 11,\"field2\": 12,\"field3\": 13}]";
and want to convert it to a JSONArray.
Thus i use the following code:
JSONArray Jarray = CDL.toJSONArray(buffer);
My Problem is now i get the following exception:
org.json.JSONException: Bad character ':' (58). at 24 [character 25 line 1]
at org.json.JSONTokener.syntaxError(JSONTokener.java:432)
at org.json.CDL.rowToJSONArray(CDL.java:113)
at org.json.CDL.toJSONArray(CDL.java:193)
at org.json.CDL.toJSONArray(CDL.java:182)
at MyDataexchange.MyCVSConverter.convertJson(MyCVSConverter.java:44)
at Mainexe.DataTest.main(DataTest.java:22)
As you can see in the stacktrace i want to use this to convert the string to .cvs at the end.
Since i dont know how to do it in a better way i'd like to know how to fix this exception.
Do i need to substitute the ':' with anything?
(Substitue ':' to ',' would produce null for example but not throw an exception, still it doesnt help me)
If yes it would be nice to tell me with what, otherwise any suggestions are welcome.
org.json.CDL is for parsing and serializing comma delimited text. However, your sample string is not comma delimited text. It's JSON. You probably wanted JSONArray Jarray = new JSONArray(buffer)
Ok, here's a better way of doing this (similar to what "guest" said):
String s = "[{\"field1\": 11,\"field2\": 12,\"field3\": 13}]";
Object obj=JSONValue.parse(s);
JSONArray array=(JSONArray)obj;
Related
I have a string str where
str = {"name":"vicky"12/3","pwd":""pwd":myPassword/123""}.
When I am passing the string in the JSONParser(org.json.simple.parser.JSONParser) I am getting the parse exception.
Here str may vary depending on different input.
Tried with JSONObject.escape(str). But then it is giving the error as Unexpected character (\) at position 1.
Please anyone suggest so that parser will parse correctly and using that a JSON object will be constructed so that json.get("name") and json.get("pwd") will return correct result where json is the object of JSONObject.
Thanks in advance.
I am trying to convert Json String into Java Object. It is working for other inputs just for one input its not working. Is their any other way to do this?
My code:
JsonParser parser=new JsonParser();
JsonObject sel=parser.parse(selectedTerritory).getAsJsonObject();
TmsMapItBuilderBean dataSet = new Gson().fromJson(sel, TmsMapItBuilderBean.class);
ArrayList dataList = dataSet.getResultList();
resultMapData = mapServiceDelegate.processAssetDataWithGeoCodeForTerritories(subId, dataList);
It shows the following error:
Error on json convert to object :com.google.gson.JsonSyntaxException:
com.google.gson.stream.MalformedJsonException: Unterminated object near
ddress' :'2500 LEE'S CHAPEL RD', "stat
Your JSON is broken. You use a character that delimits the field's with which you are encoding your object. Use an escape to encode ' as a string.
Example:
{ 'name': 'tom\'s Hut' }
However as someone pointed out ' are not meant to be used for JSON Objects. I highly encourage you to use double-quotes.
{"name": "tom's Hut"}
Would be the correct way...
For more information on JSONs see: http://www.json.org/
I'm trying to do some parsing with JSON file simply getting the key and value from JSON and passing it into jsonArray then create hashmap from array.
My code works for most of the JSON files I encounters but doesn't work for one file (its huge and somewhat confidential). The error I'm getting is
Exception in thread "AWT-EventQueue-0" javax.json.JsonException: Unexpected Char=155
at org.glassfish.json.JsonTokenizer.readString(JsonTokenizer.java:168)
at org.glassfish.json.JsonTokenizer.nextToken(JsonTokenizer.java:302)
at org.glassfish.json.JsonParserImpl$StateIterator.nextToken(JsonParserImpl.java:158)
at org.glassfish.json.JsonParserImpl$StateIterator.next(JsonParserImpl.java:183)
at org.glassfish.json.JsonParserImpl.next(JsonParserImpl.java:151)
at org.glassfish.json.JsonReaderImpl.readObject(JsonReaderImpl.java:184)
at org.glassfish.json.JsonReaderImpl.readArray(JsonReaderImpl.java:152)
at org.glassfish.json.JsonReaderImpl.readArray(JsonReaderImpl.java:127)
at parserUI.js2Properties(parserUI.java:173)
and my line 173 is this
JsonArray array = jsonReader.readArray();
Do you have any idea what is causing this? The format of json are generally as this
[
{
"key":"aaaa",
"value":"bbbbb",
"description":""
},
{
"key":"cccccc",
"value":"dddddd",
"description":""
},
]
I found which line is causing the issue
its this
"value":"查看活动",
but if then created an identical line(shown below) to replace it, then it works
"value":"查看活动",
they are identical but one works and one doesn't
...can anyone explain this?
There might be special characters or hidden characters or non printable unicode characters which might be there in input. Try to remove these type of characters with regex and then try.
The codes is like the following:
JSONObject solution = new JSONObject();
variableName = "TEST"
System.err.println("1:"+value);
solution.put(variableName, value);
System.err.println("2:"+solution);
Here is the output result:
1:{"min":10,"max":40}
2:{"TEST":"{\"min\":10,\"max\":40}"}
How can I get rid of the annoying '\'?
Thank you very much!
The reason why you are getting \ in your print line is because it is escaping the " character which is used in value. There's nothing wrong with this, it simply signifies that \" is not terminating the string and is instead a value part of that string.
Usage of double quotations is valid JSON, not single quotes - see related Q here.
But if you really want to, if you create value like this:
JSONObject value = new JSONObject("{'min':10,'max':40}");
Then you should get the desired output from your existing code:
1:{"min":10,"max":40}
2:{"TEST":{"min":10,"max":40}}
Everything is in the title :)
I'm using org.json.CDL to convert JSONArray into CSV data but it renders a string with ',' as separator.
I'd like to know if it's possible to replace with ';' ?
Here is a simple example of what i'm doing:
public String exportAsCsv() throws Exception {
return CDL.toString(
new JSONArray(
mapper.writeValueAsString(extractAccounts()))
);
}
Thanks in advance for any advice on that question.
Edit: No replacement solution of course, as this could have impact for large data, and of course the library used enable me to specify the field separator.
Edit2: Finally the solution to extract data as JSONArray (and String...) was not very good, especially for large data file.
So i made the following changes:
use a Java CSV library (for example: http://www.csvreader.com/java_csv_samples.php)
refactor code to stream data from json input source to csv output source
This is nicer for large data treatment. If you have comments do not hesitate.
String output = "Hello,This,is,separated,by,a,comma";
// Simple call the replaceAll method.
output = output.replace(',',';');
I found this in the String documentation.
Example
String value = "Hello,tthis,is,a,string";
value = value.replace(',', ';');
System.out.println(value);
// Outputs: Hello;tthis;is;a;string