Retrofit: Parse unicode characters in json - android

I have some UNICODE characters in my response like this:
"city": "Bel\\u00e9m"
and it is parsed like this:
city = "Bel\u00e9m"
But i expect it to be:
city = "Belém"
What is going wrong here and what should i do to have a properly parsed unicode character in my response?

String auto convert unicode characters to letters.
So save your response to a string variable and then make JSON from that string object.
String myString = "city": your json response;
then make json object from myString
JSonObject obj = new JSonObject(myString);
may it help you.

try this out,
Html.fromHtml(city.replaceAll("\\","\"));

Related

at user of type java.lang.String cannot be converted to JSONObject json response android

the Response From Server is:
{"message":"Login successfull","error":false,"user":"{\"DISTRICT\":[{\"Id\":1,\"Name\":\"Abbottabad\"},{\"Id\":3,\"Name\":\"Bannu\"},{\"Id\":4,\"Name\":\"DI Khan\"},{\"Id\":5,\"Name\":\"Kohat\"},{\"Id\":6,\"Name\":\"Mardan\"},{\"Id\":7,\"Name\":\"Peshawar\"},{\"Id\":8,\"Name\":\"Swat\"},{\"Id\":9,\"Name\":\"Battagram\"},{\"Id\":10,\"Name\":\"Buner\"},{\"Id\":11,\"Name\":\"Chitral\"},{\"Id\":12,\"Name\":\"Charsadda\"},{\"Id\":13,\"Name\":\"Dera Ismail Khan\"},{\"Id\":14,\"Name\":\"Hangu\"},{\"Id\":15,\"Name\":\"Haripur\"},{\"Id\":16,\"Name\":\"Karak\"},{\"Id\":17,\"Name\":\"Kolai Pallas Kohistan\"},{\"Id\":18,\"Name\":\"Upper Kohistan\"},{\"Id\":19,\"Name\":\"Lower Kohistan\"},{\"Id\":20,\"Name\":\"Lakki Marwat\"},{\"Id\":21,\"Name\":\"Lower Dir\"},{\"Id\":22,\"Name\":\"Malakand\"},{\"Id\":23,\"Name\":\"Mansehra\"},{\"Id\":24,\"Name\":\"Nowshera\"},{\"Id\":25,\"Name\":\"Shangla\"},{\"Id\":26,\"Name\":\"Swabi\"},{\"Id\":27,\"Name\":\"Tank\"},{\"Id\":28,\"Name\":\"Upper Dir\"},{\"Id\":29,\"Name\":\"Torghar\"}],\"BUSINESS_TYPE\":[{\"Id\":1,\"Name\":\"Hotels\"},{\"Id\":2,\"Name\":\"Restaurants\"},{\"Id\":3,\"Name\":\"Schools\\\/Colleges\"},{\"Id\":4,\"Name\":\"Dhaba\\\/Tea Stall\\\/Shwarma\\\/Dahi Bhalay\"},{\"Id\":5,\"Name\":\"Tandoor\"},{\"Id\":6,\"Name\":\"Juice Shop\"},{\"Id\":7,\"Name\":\"Kabab Shop\"},{\"Id\":8,\"Name\":\"Mega Marts\\\/Super Store\"},{\"Id\":9,\"Name\":\"Stores\"},{\"Id\":10,\"Name\":\"Karyana Store\"},{\"Id\":11,\"Name\":\"Fruits\\\/Dry Fruits\\\/Vegetables\"},{\"Id\":12,\"Name\":\"Meat Shop\"},{\"Id\":13,\"Name\":\"Poultry\\\/Fish Shop\"},{\"Id\":14,\"Name\":\"Whole Sale Dealer\"},{\"Id\":15,\"Name\":\"Distribution Point\"},{\"Id\":16,\"Name\":\"Wharehouse\"},{\"Id\":17,\"Name\":\"Bakers and Confectioners\"},{\"Id\":18,\"Name\":\"Small Industry\"},{\"Id\":19,\"Name\":\"Medium Industry\"},{\"Id\":20,\"Name\":\"Large Industry\"},{\"Id\":21,\"Name\":\"Cottage Industry\"},{\"Id\":22,\"Name\":\"Cart\"},{\"Id\":23,\"Name\":\"Cold Storage\"},{\"Id\":24,\"Name\":\"Slaughter House\"},{\"Id\":25,\"Name\":\"Dairy Shop and Form\"},{\"Id\":26,\"Name\":\"Canteen\"},{\"Id\":27,\"Name\":\"Others\"},{\"Id\":28,\"Name\":\"Oil\\\/Ghee Industry\"},{\"Id\":29,\"Name\":\"Ice Factory\"},{\"Id\":30,\"Name\":\"Salt and Minerals\"},{\"Id\":31,\"Name\":\"Ice Cream Factory\"},{\"Id\":32,\"Name\":\"Water Purification Plant\"},{\"Id\":33,\"Name\":\"Masala\"},{\"Id\":34,\"Name\":\"Papar Factory\"},{\"Id\":35,\"Name\":\"Papar\\\/Chips\\\/Pops Factory\"}],\"ASSISTANT_DIRECTOR\":[{\"staff_id\":1,\"staff_name\":\"Sajjad Ahmad\"},{\"staff_id\":2,\"staff_name\":\"Murad Ali\"}],\"FSO\":[{\"staff_id\":35,\"staff_name\":\"Shahab\"},{\"staff_id\":36,\"staff_name\":\"Shakeel Ahmad Khan\"},{\"staff_id\":37,\"staff_name\":\"Nawab Ali\"},{\"staff_id\":38,\"staff_name\":\"Shakeel Ahmed\"}],\"OFFENCE\":[{\"Id\":1,\"Name\":\"Adultration\"},{\"Id\":2,\"Name\":\"Mislabeling\\\/Misbranding\"},{\"Id\":4,\"Name\":\"Unhygienic Condition\"},{\"Id\":5,\"Name\":\"Banned Items\"},{\"Id\":6,\"Name\":\"Expired Items\"}],\"SAMPLE_DISCARDED\":[{\"Id\":1,\"Name\":\"Milk\"},{\"Id\":2,\"Name\":\"Meat\"},{\"Id\":3,\"Name\":\"Water\"},{\"Id\":4,\"Name\":\"Beverages\"},{\"Id\":5,\"Name\":\"Diary Products\"},{\"Id\":6,\"Name\":\"Poultry\"},{\"Id\":7,\"Name\":\"Salt\"},{\"Id\":8,\"Name\":\"Spices\\\/Masala\"},{\"Id\":9,\"Name\":\"Bakary\"},{\"Id\":11,\"Name\":\"Chips\\\/Pops\"},{\"Id\":12,\"Name\":\"Oil\\\/Ghee\"},{\"Id\":13,\"Name\":\"Ice\"},{\"Id\":14,\"Name\":\"Non-Food Colors\\\/Flavours\"},{\"Id\":15,\"Name\":\"Fruit Pulp\\\/Squash\\\/Jams\\\/Pickles\"}]}"}
What I am trying to do is:
JSONObject obj = new JSONObject(s);
JSONObject userJson = obj.getJSONObject("user");
which Give the Correct Result with Exception which is:
W/System.err: org.json.JSONException: Value {"DISTRICT":[{"Id":1,"Name":"Abbottabad"},{"Id":3,"Name":"Bannu"},{"Id":4,"Name":"DI Khan"},{"Id":5,"Name":"Kohat"},{"Id":6,"Name":"Mardan"},{"Id":7,"Name":"Peshawar"},{"Id":8,"Name":"Swat"},{"Id":9,"Name":"Battagram"},{"Id":10,"Name":"Buner"},{"Id":11,"Name":"Chitral"},{"Id":12,"Name":"Charsadda"},{"Id":13,"Name":"Dera Ismail Khan"},{"Id":14,"Name":"Hangu"},{"Id":15,"Name":"Haripur"},{"Id":16,"Name":"Karak"},{"Id":17,"Name":"Kolai Pallas Kohistan"},{"Id":18,"Name":"Upper Kohistan"},{"Id":19,"Name":"Lower Kohistan"},{"Id":20,"Name":"Lakki Marwat"},{"Id":21,"Name":"Lower Dir"},{"Id":22,"Name":"Malakand"},{"Id":23,"Name":"Mansehra"},{"Id":24,"Name":"Nowshera"},{"Id":25,"Name":"Shangla"},{"Id":26,"Name":"Swabi"},{"Id":27,"Name":"Tank"},{"Id":28,"Name":"Upper Dir"},{"Id":29,"Name":"Torghar"}],"BUSINESS_TYPE":[{"Id":1,"Name":"Hotels"},{"Id":2,"Name":"Restaurants"},{"Id":3,"Name":"Schools\/Colleges"},{"Id":4,"Name":"Dhaba\/Tea Stall\/Shwarma\/Dahi Bhalay"},{"Id":5,"Name":"Tandoor"},{"Id":6,"Name":"Juice Shop"},{"Id":7,"Name":"Kabab Shop"},{"Id":8,"Name":"Mega Marts\/Super Store"},{"Id":9,"Name":"Stores"},{"Id":10,"Name":"Karyana Store"},{"Id":11,"Name":"Fruits\/Dry Fruits\/Vegetables"},{"Id":12,"Name":"Meat Shop"},{"Id":13,"Name":"Poultry\/Fish Shop"},{"Id":14,"Name":"Whole Sale Dealer"},{"Id":15,"Name":"Distribution Point"},{"Id":16,"Name":"Wharehouse"},{"Id":17,"Name":"Bakers and Confectioners"},{"Id":18,"Name":"Small Industry"},{"Id":19,"Name":"Medium Industry"},{"Id":20,"Name":"Large Industry"},{"Id":21,"Name":"Cottage Industry"},{"Id":22,"Name":"Cart"},{"Id":23,"Name":"Cold Storage"},{"Id":24,"Name":"Slaughter House"},{"Id":25,"Name":"Dairy Shop and Form"},{"Id":26,"Name":"Canteen"},{"Id":27,"Name":"Others"},{"Id":28,"Name":"Oil\/Ghee Industry"},{"Id":29,"Name":"Ice Factory"},{"Id":30,"Name":"Salt and Minerals"},{"Id":31,"Name":"Ice Cream Factory"},{"Id":32,"Name":"Water Purification Plant"},{"Id":33,"Name":"Masala"},{"Id":34,"Name":"Papar Factory"},{"Id":35,"Name":"Papar\/Chips\/Pops Factory"}],"ASSISTANT_DIRECTOR":[{"staff_id":1,"staff_name":"Sajjad Ahmad"},{"staff_id":2,"staff_name":"Murad Ali"}],"FSO":[{"staff_id":35,"staff_name":"Shahab"},{"staff_id":36,"staff_name":"Shakeel Ahmad Khan"},{"staff_id":37,"staff_name":"Nawab Ali"},{"staff_id":38,"staff_name":"Shakeel Ahmed"}],"OFFENCE":[{"Id":1,"Name":"Adultration"},{"Id":2,"Name":"Mislabeling\/Misbranding"},{"Id":4,"Name":"Unhygienic Condition"},{"Id":5,"Name":"Banned Items"},{"Id":6,"Name":"Expired Items"}],"SAMPLE_DISCARDED":[{"Id":1,"Name":"Milk"},{"Id":2,"Name":"Meat"},{"Id":3,"Name":"Water"},{"Id":4,"Name":"Beverages"},{"Id":5,"Name":"Diary Products"},{"Id":6,"Name":"Poultry"},{"Id":7,"Name":"Salt"},{"Id":8,"Name":"Spices\/Masala"},{"Id":9,"Name":"Bakary"},{"Id":11,"Name":"Chips\/Pops"},{"Id":12,"Name":"Oil\/Ghee"},{"Id":13,"Name":"Ice"},{"Id":14,"Name":"Non-Food Colors\/Flavours"},{"Id":15,"Name":"Fruit Pulp\/Squash\/Jams\/Pickles"}]} at user of type java.lang.String cannot be converted to JSONObject
at org.json.JSON.typeMismatch(JSON.java:100)
I Tried a lot but can not find the solution.
The problem is that the value for the key "user" is not actually a json object, it is a String that looks like a json object. This is what you have, formatted and trimmed a little bit:
"user":"{\"DISTRICT\":[...]}"
If this were a json object, it would look like this instead:
"user":{"DISTRICT":[...]}
The double-quotes surrounding the curly braces, as well as the backslashes escaping the double-quotes inside the curly braces, are clues that you're actually working with a String here.
What you could do is retrieve the value of "user" as a String, but then convert it to a json object:
JSONObject obj = new JSONObject(s);
String userString = obj.getString("user");
JSONObject userObject = new JSONObject(userString);

how to get data from serialize json array

{
"id": "1",
"name": "test1",
"data_city": "a:35: {i:22;s:6:\"61,800\";i:23;s:6:\"61,800\";i:24;s:6:\"61,800\";i:25;s:6:\"61,800\" ;i:26;s:6:\"61,800\";i:27;s:6:\"61,800\";i:28;s:6:\"61,800\";i:29;s:6:\"61,800\";i:30;s:6:\"61,800\";i:31;s:6:\"61,800\";i:54;s:6:\"61,800\";i:16;s:6:\"61,800\";i:32;s:6:\"61,800\";i:52;s:6:\"61,800\";i:21;s:6:\"61,800\";i:33;s:6:\"61,800\";i:37;s:6:\"61,800\";i:34;s:6:\"61,800\";i:36;s:6:\"61,800\";i:38;s:6:\"61,800\";i:41;s:6:\"61,800\";i:35;s:6:\"61,800\";i:39;s:6:\"61,800\";i:40;s:6:\"61,800\";i:42;s:6:\"61,800\";i:44;s:6:\"61,800\";i:43;s:6:\"61,800\";i:46;s:6:\"61,800\";i:45;s:6:\"61,800\";i:47;s:6:\"61,800\";i:49;s:6:\"61,800\";i:53;s:6:\"61,800\";i:50;s:6:\"61,800\";i:48;s:6:\"61,800\";i:51;s:6:\"61,800\";}"
}
This is my json response
i need to get data from string data_city
First of all, your JSON is not valid - probably a copy paste error.
Anyway, there are several ways to do this. One option is to use the built in JSONObject type:
String jsonString = "YOUR_JSON_STRING";
JSONObject objJSON = new JSONObject(jsonString);
String dataCity = objJSON.getString("data_city");
Hope this helps. Be sure to run your JSON String through a validation tool first. This one works nicely: https://jsonformatter.curiousconcept.com/

Android Convert String to JSONObject

I want to convert a string to JSONObject. Below is sample code.
String str = "{"time": 1449838598.0999202, "Label": "Shirt", "Price": 52}";
JSONObject obj = new JSONObject(str);
But after conversion time becomes 1.4498385980999203E9.
Any Help will be appreciated.
Thanks
If you really want it to be formatted like that then make it into a string instead of a number. Just add double quotes around the item. But if your using it for a calculation on the other end, i would leave it as is as you'll have to convert it back to a int anyway. If you want to use gson as an alternative check here: How to prevent Gson from converting a long number (a json string ) to scientific notation format?
Write it like this in double quote. "1449838598.0999202"
Like Abhishek said, write time & price in double quote as "1449838598.0999202" and "52".
Then you can save the string to Gson, and then convert it to Json.
String str = "{"time": "1449838598.0999202", "Label": "Shirt", "Price": "52"}";
Gson gson = new Gson();
String json = gson.toJson(str);

How to get a JSON with dollar sign

suppose I have {"data": {"243232": {"id": "testid","name": "test" } }}
so how to get correct value thanks.
you cant directly pass above String with Gson as "243232" is number and we cant declare variable which start from number, So for parsing this you must modify the string with some notification
i.e. {"data": {"temp243232": {"id": "testid","name": "test" } }}
here i modified string manually "243232" by "temp243232", now you can parse it
You can use a JSON parser for Java like Google GSON. This works best if the class whose object your data in json represents, is known to you.
String str = ""; //contains your json string
Gson gson = new Gson();
MyClass var = gson.fromJson(str,MyClass.class);
where MyClass will be a class which represents the object.
Then you can access data just like you'd do from a data structure in your program, it'll be something like var.data.

Trouble with Android JSONObject from Grails webservice

Okay I am quering data from a Grails webservice that returns JSON. The JSON when viewed with the JSONViewer app parses fine. When I take that same string and use JSONObject(string) in my Android app I get "value of String cannot be converted to JSONObject."
Here's my JSON string
[[{"class":"mygrails.TopTen","id":491,"ttAmount":14200000,"ttMlId":402,"ttRank":1,"ttWeekId":1108},{"class":"mygrails.MovieList","id":402,"mlApproved":1,"mlApprovedId":5,"mlMovieId":"GNOMEOAN","mlReleaseDate":"2011-03-08T07:41:45Z","mlTitle":"Gnomeo and Juliet","mlWeekId":1106}]]
Now the JSON is comes from the standard JSON conversion of a SQL data using render from the groovy file through the import grails.converters.JSON.
... //(call to render JSON in the groovy file)
def a
a = Table.findAll("from someTable as st where st.id=" params.id)
render a as JSON
...
So I am not sure what I doing wrong and why the JSON looks a little off to me. (still new to JSON)
In json if you see "[]" means its a json array and if you see "{}" it is an json object. Both of then can have the other nested inside then.
In your case the string the starts with json array.
So try something like the following
String str = "[[{"class":"mygrails.TopTen","id":491,"ttAmount":14200000,"ttMlId":402,"ttRank":1,"ttWeekId":1108},{"class":"mygrails.MovieList","id":402,"mlApproved":1,"mlApprovedId":5,"mlMovieId":"GNOMEOAN","mlReleaseDate":"2011-03-08T07:41:45Z","mlTitle":"Gnomeo and Juliet","mlWeekId":1106}]]";
JSONArray jsonArray = new JSONArray(str);
jsonArray = jsonArray.getJSONArray(0);
JSONObject jsonObject = jsonArray.getJSONObject(0);
String class = jsonObject.getString("class"); // class will value "mygrails.TopTen"
Try to create an JSONArray from the String instead of JSONObject. I didn't test this but that should do the trick: you have two nested arrays that contain then actual data.
Check out your JSON online with http://jsonformat.com/
http://www.freeformatter.com/json-formatter.html
JSON Viewer
http://jsonviewer.stack.hu/
Paste your text in there and you can see what you should parse:

Categories

Resources