I'm trying to parse a JSON response, but all the fields are u' prefixed. I did some search its Unicode format, not UTF-8 realised, but how to parse it finally in Android?
"{u'response': [{u'status': u'ONGOING', u'challenge_type': u'Monthly Challenges', and so on
Online JSON Viewer is formatting it but since it's Unicode, so all are chars, that's not the way we can parse JSON response.
In my code even creating a JSONObject is also throwing an exception.
Any solution or hint towards correct path would be very helpful.
JSON link
For those who are giving a downvote please leave a solution before the downvote, would really like to know what I missed out.
That is not JSON. That's a Python dict literal. Although Python and JS literals are superficially similar they are far from cross-compatible.
You could parse it from Python using ast.literal_eval, but that's not much use from Android, and it would be a very strange web API that chooses Python literals for its output format. If you have a web service that is supposed to be returning JSON but is actually returning this, it's broken.
Related
I cannot find any explanation about why we use JSON when parsing it in Android?. Are we required to use it or is this just optional? What's the advantage and disadvantage of using it to extract data from the internet? Do we have to encode every data from PHP & MySQL into a JSON Format?
See this question. JSON is known as JavaScript Object Notation, it's a lot easier to parse then XML data, and there are libraries to help you retrieve the values from a JSON structure. As for the PHP & MySQL question, Android doesn't use MySQL, it uses SQLite instead. Hope this helps.
JSON is just a format to represent Objects in textform. Its used often in Android because its nice to work with and has many libraries to help parse it.
Sometimes other formats are used like XML or something else to represent the data.
You can use any communication protocol you want, json is just an accepted standard. You can't just 'dump the mysql database' to the app, there has to be some sort of communication protocol that the server and client agree on to transport the data.
Some alternatives are xml or a binary format (such as protobuf).
I'd recommend json due to the abundance of tools and literature on it.
(Heres a blog post I wrote on using Jackson to parse json in Android http://shmuelrosansky.com/jackson/android/2015/07/20/jackson-android/)
I have worked with parsing JSON and I want to create a JSON file online. Have the website support to create file JSON online?
There is a nice online JSON editor at http://www.jsoneditoronline.org/.
Please note that I had never heard of such tools before reading this question, because I usually never write JSON by hand except for a few special cases. JSON is meant before all as a language that can be easily generated and parsed by machines.
(The special case I mentionned are some configuration files, and using nicely formatted and commented JSON, because the compact notation is definitly not human-friendly.)
I am a newbie in Android , I am doing a project where json parsing is done by calling a URL. I am able to do this by using built-in json parser provided by android.
But when i try to parse huge json response ,it fails. I got to know about another parser, "Jackson parser".
I googled a lot,but with no luck.
So , can anybody suggest me a way to parse json using Jackson parser through url?
OR
Provide me some example links, if possible?
I think, It will be helpful to other newbies like me.
You might want to give the package org.json a try. It is provided directly from the Android sdk.
JSONObjects can directly be parsed and provided to your objects i.g. via the constructor.
Try using thed Gson lib to deserialize json objects. Less painful to use, and it supports large objects quite well.
Since two days I am trying to consume a WCF (.NET) Soap Service and serialize it's response without success. I am getting a correct response (I had to put it on pastebin: SOAP Response Example), but KSOAP2 is not able to handle .NET Datasets correctly.
I already consultated various articles about this specific problem, but none has a .NET Dataset to handle. The main article which gave guideance was an
article by IBM "Working with XML on Android"
I tried following steps to parse my data without success:
Parsing with SAX (android) -> Seems not to work with this complex document because of the different namespaces.
with DOM Object(android) -> NullPointerExeption (dooh!)
with Digester (dom4j) -> NullPointerException (arggh!)
method suggested by helloandroid.com "Using ksoap2 for android, and parsing output data"
Some questions:
- The returned response is a normal XML, but actually it includes a .NET Dataset. Has anyone had success to parse data out of such a response?
- Is there a way to make KSOAP2 not to trying to parse the data? It returns a rubbish SoapObject, which is unreadable. I would like just the contents of the SOAP body. Is there a way to intercept that?
- Do you have any other hint?
With ksoap2 you can set the envelope debugging to true and then get the response dump, which will contain the full xml.
However what makes you think that the SoapObject returned is unreadable. Check it out in a debugger and you will find that everything is there and you can just parse it out using getProperty("propname") and getAttribute("attribute) which in turn are either SoapObjects again if they are nested or contain actual values if they are leaf nodes.
Check out some of the links on the wiki to http://code.google.com/p/ksoap2-android/
I have used DOM and SAX so far to parse XML documents. DOM had a few issues on Android and I had to handle some bugs in the API. SAX seems to be better (and leaner if you only read). I have not used ksoap, but did everything hand crafted. Though I am not sure where the problem with that .NET thing is, I wouldn't see an issue using SAX or DOM. Can you comment why you think SAX won't work because of namespaces?
A.
One of my solutions was: http://vtd-xml.sourceforge.net/
I hope someone will find something more suitable.
I have the same problem. First I used vtd-xml. It was working without problems but it is a bit slow. Now I switched to standard Java SAX (not Android SAX implementation) and it works ok.
In the Android application I am building, I want to be able to communicate with a local server developed in Django. (Basically a login page and a home page populated with posts and images from users) So do I need to use XML Parsers for the parsing the response from a Django server or is it possible for the server to respond with strings which can be directly used? Also what about images?
Is the JSON or XML Parser easier and robust to use in Android? The responses would be basically like tweets with a username, image and message. I was thinking of using the SAXParser. Any better alternatives?
Regards,
Primal
Android has built in libraries for parsing both JSON and XML.
In my opinion, the easier (and better) one would probably JSON if you're just looking to output the serialized version of your models.
Some relevant links:
JSON:
https://developer.android.com/reference/org/json/JSONObject.html
XML:
https://developer.android.com/reference/org/xmlpull/v1/XmlPullParser.html
https://developer.android.com/reference/javax/xml/parsers/package-summary.html
Edit: In response to the last part of your question, yes, you can just output strings. Depending on the complexity of your data, you'll end up making things harder for yourself. Parsing JSON on Android is super easy. Just do it.
SAXParser is very easy to use, it just calls a method when it enters a node with the name of the node and its arguments.
So yes, using SAX is a good idea.