3253

Json parsing in android 2

Question:

I am calling a web service and receiving this JSON but instantiating the JSONObject class is throwing error, as you can see it contains Persian character (UTF-8) which I don't think could be reason of this problem ,

JSON:

{"teriffs": [ {"name":"برنز","id":"1000","prices":"3;400000-12;600000"}, {"name":"برنز","id":"1000","prices":"3;400000-12;600000"}, {"name":"نقره ای","id":"1002","prices":"3;700000-12;1000000"}, {"name":"نقره ای","id":"1002","prices":"3;700000-12;1000000"}] }

ERROR:

<blockquote>

org.json.JSONException: Value {"teriffs": [{"name":"برنز","id":"1000","prices":"3;400000-12;600000"},{"name":"برنز","id":"1000","prices":"3;400000-12;600000"},{"name":"نقره ای","id":"1002","prices":"3;700000-12;1000000"},{"name":"نقره ای","id":"1002","prices":"3;700000-12;1000000"}]} of type java.lang.String cannot be converted to JSONObject

</blockquote>

CODE :

try { JSONStringer requestMsg = new JSONStringer().object().key("Ticket").value(TempUtil.UID).endObject(); char[] c = CallServiceHelper.getCallService(requestMsg, "/WWWServices.svc/GetTeriffs"); if(c!=null){ JSONObject array = new JSONObject(new String(c)); System.out.println(array.toString()); return array; } else { return new JSONObject(); } } catch (Exception e) { e.printStackTrace(); }

block

Answer1:

<strong>Reason:</strong>

Some character cannot be mapped using 'MacRoman" character encoding. Either change the encoding or remove the characters which are not supported by the "MacRoman" character encoding.

<strong>Edit</strong>

String jsonString = " {\"teriffs\": [{\"name\":\"برنز\",\"id\":\"1000\",\"prices\":\"3;400000-12;600000\"},{\"name\":\"برنز\",\"id\":\"1000\",\"prices\":\"3;400000-12;600000\"},{\"name\":\"نقره ای\",\"id\":\"1002\",\"prices\":\"3;700000-12;1000000\"},{\"name\":\"نقره ای\",\"id\":\"1002\",\"prices\":\"3;700000-12;1000000\"}]}"; try { String UTF8String = new String(jsonString.getBytes("UTF-8")); JSONObject object = new JSONObject(UTF8String); JSONArray array = object.getJSONArray("teriffs"); for(int i=0;i<array.length();i++){ JSONObject jsonObject = array.getJSONObject(i); Log.d("ID", jsonObject.getString("id")); Log.d("NAME", jsonObject.getString("name")); Log.d("PRICES", jsonObject.getString("prices")); } } catch (JSONException e2) { // TODO Auto-generated catch block e2.printStackTrace(); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); }

Recommend

  • 200kb image to base64 cannot send to web service
  • Showing Error :Attempt to invoke interface method 'int java.util.List.size()' on a null ob
  • Android json parse not working
  • java.lang.String cannot be converted to org.slf4j.Marker
  • AngularJS - ngBind and Bootstrap Switch
  • python struct.pack(): pack multiple datas in a list or a tuple
  • Is a .txt file created in VB different than one I'd randomly create?
  • Default parameter as generic type
  • Scala multiline string placeholder
  • saving file generated by TCPDF
  • How to view images from protected folder with php?
  • Button click event not firing in jQuery
  • rspec simple example getting error on request variable in integration test
  • How to handle images sent by a mobile device?
  • d3 v4 drag and drop with TypeScript
  • JSON response opens as a file, but I can't access it with JavaScript
  • Play WS (2.2.1): post/put large request
  • DomPDF {PAGE_NUM} not on first page
  • Lost migrations and Azure database is now out of sync
  • Deselecting radio buttons while keeping the View Model in synch
  • Getting last autonumber in access
  • Different response to non-authenticated users and AJAX calls
  • Volley JsonObjectRequest send headers in GET Request
  • JavaScriptCore crash on iOS9
  • How to set my toolbar fixed while scrolling android
  • Importing jscolor library in angular 2
  • QuartzCore.framework for Mono Develop
  • Warning: Can't call setState (or forceUpdate) on an unmounted component
  • AT Commands to Send SMS not working in Windows 8.1
  • Arrays break string types in Julia
  • How to format a variable of double type
  • VB.net deserialize, JSON Conversion from type 'Dictionary(Of String,Object)' to type '
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • How can I get HTML syntax highlighting in my editor for CakePHP?
  • How do I configure my settings file to work with unit tests?
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • apache spark aggregate function using min value
  • Is it possible to post an object from jquery to bottle.py?
  • Sorting a 2D array using the second column C++
  • Binding checkboxes to object values in AngularJs