Android Parse a JSON String With Quotes in Keys and Values - android

Android JSON parsing is rather straightforward until it comes to have json reserved characters in your keys/values. I have JSON coming from an HTTP socket whose response is put into a string variable. It looks like this
{"ZboAdtPw4bA":"Ben Heck"s PlayStation 4 Slim Teardown","iC4qIx72_Cc":"Ben Heck's Xbox Slim Teardown"}
See the double quotation in the first value? It even screws up on StackOverflows web page. How am I supposed to escape/prevent this from happening? If I do a:
response = response.replace("\"", "");
Then all the double quotation get replaced, not just the ones in the key/value pair. This is because its all contained in one string at the moment. I am wondering if there is an easy way to do this with android. And of course, java answers are acceptable to. Now I could do this since its just a single dimensional key/value pair easily, I may not even need JSON, but I would like to adhere to standards.

you are simply trying to ruin the basic of a JSON .
you simply add
"/"" to the java code .
other than that its not possible for the parser to differentiate between the quotations from the JSON format or the quotations in the string .

Related

JSON array order

I am using PHP as a middleman to access a MySql database and it returns the result of the query as a json string using json_encode, then display it within the TableLayout of the app, this is why order is important so I can line up the data and the headers.
After some research I found out that json does not enforce order so any time I call new JSONArray(result) it scrambles the json returned by PHP. Is there any way to preserve the order of the returned string? Or maybe I'm using the incorrect data structure on either end.
Relevant PHP result:
[{"FIELD1":"vsa","FIELD2":"dfs","FIELD3":"dsfa","FIELD4":"adsf","FIELD5":"23","ZIPCODE":"asdf","USERNAME":"asd","PASSWORD":"as","DATE1":"dsfa"}]
Relevant Android Result After JSONArray(result):
[{"ZIPCODE":"asdf","DATE1":"dsfa","FIELD3":"dsfa","FIELD2":"dfs","FIELD5":"23","FIELD4":"adsf","USERNAME":"asd","FIELD1":"vsa","PASSWORD":"as"}]
I believe the reordering inside a JSON object is due to the fact that JSON objects are key/value pairs (not an indexed array), which by default are unordered. However, the JSON array is an ordered sequence of values (JSON objects).
Don't rely on order!
Source: http://www.ietf.org/rfc/rfc4627.txt
I've never seen new JSONArray(String) change the order of anything, and I've used it a lot. However, what you have seems to be an array of length 1. Using myJsonArray.getJsonObject(0).getString("ZIPCODE") should still return the correct data, and as long as you query in the correct order (FIELD1, FIELD2, FIELD3, etc), you should be fine.

Comparison - XML with JiBX or JSON with Jackson?

I need to import data from a file to my application. The obvious choices are XML and JSON. I have heard that JSON is lightweight and when parsed with Jackson, it gives good performance. But I have also heard that JiBX for XML is fast and uses XMLpull to give good performance. I would like to know which option to go for and why?. Can I get a speed comparison of XML with JiBX and JSON with Jackson? Also, I want to know if Google Gson is better than Jackson for JSON parsing.
Json is the light weight.If you want to use large documents use, JSon with Jackson.
Excellent explanation been given in this article(especially read Note:). Xml you have
different types like DOM,PULL and SAX.But as per my knowledge, JSON is the best. For Large
documents,prefer Jackson. http://www.javabeat.net/2011/05/androids-json-parser/
For Jackson and gson. Have a glance of this link.
Jackson Vs. Gson
So when comparing with xml and json,i always suggest you to use json, since it's a light weight data for android. So it will be fast in loading and display the data. And gson,
it depends based on your project. Please see the above link comparsion.you will cleanly understand.
In addition, Jackson can also do XML if that's what you need, with https://github.com/FasterXML/jackson-dataformat-xml
I also agree in that Jackson+JSON will be faster than any of XML-based solutions (as per https://github.com/eishay/jvm-serializers). JibX is not bad and probably is fast enough for most uses (as do many, many other options). But if speed is your thing, Jackson will be faster of choices you mention.
I agree that in pure performance, JSON is going to be faster than JiBX.
You choice of tools should depend on the data you are transferring.
Use JiBX if you have a concrete data definition. JiBX is especially good at creating and retrieving complex data objects. JiBX will make sure your data is correct and automatically convert your data to and from Java objects.
Use JSON if you want more flexibility. JSON doesn't check to see if your data is correct.
For example, when you create an object in JiBX:
Person person = new Person();
person.name = "Don";
person.age = 52;
When you retrieve the information in JiBX:
System.out.println("Name: " + person.name);
System.out.println("Age: " + person.age);
In JSON, your code would look like this:
JSONObject person = new JSONObject();
person.put("name", "Don");
person.put("age", new Integer(52));
To retrieve the transmitted information:
String name = person.get("name");
long age = person.get("age");
As you can see, the JiBX code is nicer to read, but the JSON is more flexible since you don't have a schema definition.
In either solution, your code is exactly the same in your Android client and your java service/server.
I hope this helps.
Don Corley - JiBX contributor

Converting multiple datas into a single string for uploading using json in android

I have to submit data from 30 pages into the server.These datas from 30 pages are to be made into a single string and that i have to upload that single string into the server using json.
Each page may contain many answers tht may be either in plain text(value we receive from edit text),from check boxes(yes or no) and so on.....please suggest me a way to add all these data into a single string and upload it using json.
Based on the comment I suspect that you believe that you need to treat these "pages" as strings that you concat. However, what I think you're overlooking is that JSON is pretty versatile in how you add objects to it.
So, let's say you have the thing that you want to ship to your server and you call it
JSONObject myEntireFile = new JSONObject();
you can now add stuff to it at any time like this...
JSONObject page1 = new JSONObject();
myEntireFile.put("page1", page1);
meanwhile you can put whatever you want IN page 1 (cause that's just another serialized container).
You can keep doing this until you're ready to send it, at which time you just call
myEntireFile.toString();
which will convert your object into one long, well formatted, JSON string, that you can then open store for later use.

android - simple way to extract a key-value pair from a json string?

Suppose I have a json string like this:
{ ... "key1":"value1"; ... }
with a key1-value1 pair somewhere deep down the json structure (which includes other things such as array, dictionary, etc...). I don't know exactly (and don't care) how the exact structure of the json is.
Is there a simple way to extract the "value1" ? (if there are 2 "key1" in the json string then I just need the first one).
As far as I know, you have no chance of doing it manually.
If you really don't know what's the structure of the JSON string you're expecting, you can try a graph search approach, such as DFS (http://en.wikipedia.org/wiki/Depth-first_search).
For every key, check if it is an array.
If so, go inside and repeat the procedure. If nothing was found in a given array, backtrack.
Interrupt your process once you have found your key.

parsing html data in android

I am making an application in which i have to parse HTML data. I have got data that i want. But data is repeating five to six times. I m saving data into string, but when i am printing this string there is no repeated data. e.g data having 23 values and it is repeating five or six times. I have entered static string it is displaying fine.
Here is code:
doc = (Document) Jsoup.connect("http://altoona.craigslist.org/search/cta?query=Ford+WINDSTAR&srchType=T&minAsk=&maxAsk=").get();
System.out.println("*****DOC*****"+doc);
s1=doc.getAllElements().text().toString();
System.out.println("**************S1*************"+s1);
Please help me where m doing something wrong.
try this one http://htmlparser.sourceforge.net/
HTML Parser is a Java library used to parse HTML in either a linear or nested fashion. Primarily used for transformation or extraction, it features filters, visitors, custom tags and easy to use JavaBeans. It is a fast, robust and well tested package.

Categories

Resources