I was trying to usefully answer this question, and figured that the questioner would want to do the conversion of the .Net-style JSON dates ("/Date(######)\/") to dates via a reviver, but looking at the documentation it's not clear to me that Android supports the concept of a "reviver". Does it?
The answer would appear to be: No, not directly. But yes, you can do it (or a form of it) without too much work:
No, in that JSONObject, JSONArray, JSONTokener, JSONReader, etc., don't seem to support the concept directly.
Yes, in that you could probably support a simple form of reviving (such as a context-free substitution of strings in a given format with corresponding Date objects) with little effort via JSONReader, or even (with just a little more work) create a fully-fledged reviver-enabled deserializer (complete with getting the order of things right, etc.). So not built-in, but not from scratch.
Related
I am still new to android and there is probably an obvious answer to my problem. But I have not found the correct question to ask or google for.
What I want to do is read in a xml representation of a (list of) java object(s) and have the objects accessible from my activity.
Is this possible with android resources?
After some reading through the internets the best solution I found was to write the objects as json string and save them in res/values.
And then use something like Gson to parse them as java objects.
But there has to be a better way.
What would be the standard way to approach this?
To add some context: I am programming a small android game and would like to have a research-tree.
Each research item has properties like a name, costs, effects and of course a list of references to other research items as preconditions.
I would appreciate any ideas and suggestions.
If you don't want to use 3rd party solutions, the recommended way is to use the platform's XmlPullParser:
https://developer.android.com/training/basics/network-ops/xml.html
I have one file which is create in iPhone using serialization object. and i want to do read in android. Is it possible or not? please reply fast in advance thanks
I am assuming that the serialized data is in a pList.
See this http://code.google.com/p/plist/
You should be able to read any file generated by another language. It's better if there's a library out there for it. It'll get complicated if the file uses arrays, especially dynamic ones. This may require some fun time with a hex editor. I can't guarantee reliability with this method though if the data isn't simple. Just realize that this may end up being a tricky task
It's better to have a file spec made. You don't have to use a binary file, as you can use something like JSON or XML if it's all text-based information.
I would like some advice. I'm going to be using an sqlite database that will be pulling down information from my server and then saving it in the DB then displaying it. Could someone advise me of the best way to populate the DB, should I...
Use a http request and return a string de-liminated with say a | and use a loop to write to the data base.
Use a JSON to retrieve the information and then store it in the database.
The information is going to be just text and some fields will contains links to images I want to then download (get to that later). Just wanted some advice on best practices. I have done some searches on SO and other sites but can't find much advice. Also as a side note any examples you know of that are good for noobs :)
Based on what you write here I would pick JSON.
To core points:
JSON is a standard format.
Android ships with a JSON lib (org.json) making it easy to handle it (encode / decode data).
JSON is known by a large community so you can ask questions and get them answered rather easily. With a custom format you cannot tag the question as 'json' here at SO... ;-)
Using standard formats and libraries helps you to avoid designing and implementing this stuff yourself, which makes your software more robust.
Sometime later you might need to add more complex data to your project. By that time it will be rather straightforward to use JSON's array and objects. With your private scheme you will have to add this capability to it and extend your parsing code. That can easily introduce subtle bugs. Or you might decide at that point that it is too hard with your custom format and decide to move over to a standard like JSON, XML, etc. At that time it costs you much more to shift over than if you start with a standard format. Consider time invested to write and test the current code and then the extra time to change to the standard format for the current system.
Many times my client ask me whether they will deliver data via XML feed or JSON strings. I usually say:
XML if you already have a feed and do not have a web developer who will create script for generating JSON strings
JSON if you do not have any feed and need to create any from the scratch
What do you say? Do you think that delivering data via XML feeds is obsolete and that XML is over-complicated and too heavy?
Should I advise all clients (for the sake of the future) to move onto JSON way of delivering data?
EDIT
From another discussion https://stackoverflow.com/questions/2636245/choosing-between-json-and-xml I can see that JSON is advised for web services, which is the most used case scenario in my clients. It seems that I was advising them properly.
What is they want to pass news articles onto a mobile device - shall I advise XML of JSON?
What about post&get cases when I need to post some data and the to get the response which will be displayed on user's mobile device - XML or JSON?
If the consumers are browsers or mobile devices, I would recommend JSON.
Faster
Lighter
Native parsing support
If the consumers are other programs, I would recommend XML
Can be validated easily
Code generators available to make programming easy and is less error-prone
JSON - if you have a choice :) Google GSON is a serious help there.
We Use JSON: If we want to serialize a data structure that’s not too text-heavy and all you want is for the receiver to get the same data structure with minimal effort
We use XML:If we want to provide general-purpose data that the receiver might want to do unforeseen weird and crazy things with, or if you want to be really paranoid and picky about i18n, or if what you’re sending is more like a document than a struct, or if the order of the data matters, or if the data is potentially long-lived.
This discussed topic might help you .
I agree with all the other recommendations for JSON, but for me the main reason for going with JSON is it's far easier to process on the server especially if you are using a language that supports the JSON structure natively (e.g NodeJS or Python).
I would not say XML is obsolete though. The one obvious case where XML wins is readability. As a programmer I would say JSON is just as readable but I've worked with a lot of people (mainly web designer types) who prefer the look and feel of XML, probably because they are already intimately familiar with HTML.
I agree with your assessment really. Json is easier (for a human) to read, more intuitive and lightweight. XML is better if you have lots of existing XML solutions/interfaces that you're plugging in to. I see XML as the established, mature heavyweight of structured documents, but you don't always need an established, mature heavyweight. It all depends on the use case.
My company wants to publish a library for android, so that other people can integrate our content easily in their custom apps.
Now I am still very uncertain how I should provide the content. Right now I am using POJOs to provide the data in my project.
I was following the debate "POJOs vs. Cursors" which turned out in favor of Cursors. I also have seen an example where somebody stores the parsed resource in a SQLLite-DB and accesses it later through Cursors.
I know that Cursors have many advantages compared to POJOs. But would you like to mess with cursors when you need to implement a library? Some of you guys might have written such libs as well. What did you use and why?
How other libs are doing it...
Facebook seems to use POJOs:
Response format: The server response is a JSON string. The SDK provides a Util.parseJson() method to convert this to a JSONObject, whose fields and values can be inspected and accessed.
We used the Cursor approach in our last project, and found it quite cumbersome. Especially having to check whether there's actually something in the Cursor, iterating over it, finding the right indices for values, closing it properly.. not something I like to do over and over again. Especially the whole index stuff tends to break quite easily, especially since you can't define constants for it if you're actually making use of projections.
A good approach would probably be to use Cursor backed POJOs, at least when it comes to collections. That way the data could be read on demand. You still would have to find a sensible way to close the cursor then, though.
In case of single objects, I say to hell with projections and just dump everything it into a POJO.