I'm trying to write/read a json object to/from a file.
The json structure is complex and is dynamically generated.
For small json object I would just transform json to string then do string writing/reading. This causes out of memory issue when the json gets too large.
How do I stream the JSONObject directly to a file, and stream back the JSONObject directly from the file?
Having a quick look at the Android api, it seems that you want functionality similar to JsonStringer (https://developer.android.com/reference/org/json/JSONStringer.html) - but it doesn't seem like it is capable of streaming the results. Other than that, I think you'll be bound to either finding a suitable library (possibily https://github.com/yonik/noggit) or implementing your own json streamer (which I doubt would be that hard).
I suggest you use GSON, Google's library to handle JSONs. I use it a lot for converting JSON web responses into classes.
https://github.com/google/gson/blob/master/UserGuide.md
Related
I am trying to create a raw JSON viewer for JSON strings. I understand there are apps that already do this but I am looking to load JSON from my server and display it for an internal tool for my company.
I am able to pretty print the JSON values, but this can still be difficult to read. Are there any Android libraries available for displaying color-coded JSON or JSON that can have its items expanded? If not can anyone suggest a way to do so? I've looked into ExpandableListView but this only goes up to two levels deep
Recommend you to see GSON, it simplify JSON.
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 need to know the purpose of using JSON in android ?
Please anyone tell me in a simple way...
Thanks
The same reason you'd use it on any platform. JSON is a way of storing and expressing information. It uses attribute-value pairs in a hierarchical structure. In Android specifically, you may need to download some information from a database, which could be stored in JSON and then read by your app. Alternatively, you could store data locally in JSON but there are probably better and more efficient ways to do that if you're not sending data across a network.
https://en.wikipedia.org/wiki/JSON
JSON is very light weight, structured, easy to parse and much human readable. JSON is best alternative to XML when your android app needs to interchange data with your server
For example, you can get data Json if you work with database. Or if you work with some API's then you can get data in format Json.
For example an app could fetch data from a server. When using JSON to get the data, the traffic is quite small and the app can easily work with it.
For example you have a server with a database with recipes, and your app displays recipes, the app could ask the server for recipes, and it gets a JSON in return. for example:
{
name: 'Cookies'
ingredients: { 'Butter', 'Eggs', ... /* I don't know, I'm not a chef :D */
...
}
The app can then just read the properties and display them in a neat list ;)
JSON (JavaScript Object Notation) is a lightweight format that is used for data interchanging. It is also a subset of JavaScript's Object Notation (the way objects are built in JavaScript
Pls go through this link: http://www.copterlabs.com/blog/json-what-it-is-how-it-works-how-to-use-it/
JSON stands for JavaScript Object Notation
JSON is lightweight text-data interchange format
JSON is language independent *
JSON is "self-describing" and easy to understand
* JSON uses JavaScript syntax for describing data objects, but JSON is still language and platform independent. JSON parsers and JSON libraries exists for many different programming languages.
Using JSON in Android is not different than using it on any other platform. The main advantage of the format (in comparison to XML for example) is the small size of the data. This is very important for mobile devices due to the scarce resource those application use - i.e. your mobile app should be able to run with little memory usage, slow internet connection and so on.
Besides Android's framework has built-in tools for parsing / creating JSON objects. Thus it is both easy and efficient to use JSON rather than XML. If you have any project specific reason to prefer another data presentation format - don't worry. It is perfectly fine NOT to use JSON as long as some other format is more suitable for your project.
In short JSON is usually the right choice due to its small footprint and easy of use.
I am trying to upload & retrieve data with the server.
Which is the best way to retrieve data using xml or json ?
Thank you
As i have used both XML and JSON, and also used all parser including SAX, DOM, Pull Parser. I have also developed web-service for JSON and XML both.
So I suggest you to go with JSON. why?
Because webservice for JSON response seems to develop easily, we don't need to do anything for creating JSON response, we just have to do json_encode() in PHP.
And while in Android, we can parse the JSON string easily by writing less code.
since it supports both.It depends on your feasibility.better go with json as it is easy to implement
I've just done an app that exchanges data over the wire with an API using Google's protocol buffers. There's a neat Java library available from Google for generating the java objects based on your proto files and other libraries to use for parsing an input stream into objects.
Really fast, very low bandwidth, though a bit of an overhead to setup and there's no readable data that you can drop into Notepad to view if you're having trouble.
IMHO the point of choosing one of them goes mainly down to the data size that needs to be transferred to the client. Obviously that should be as small as possible and so the preferred choice is usually
JSON
Google Protocol Buffers
..because they are much more concise than XML.
For data-oriented applications, I prefer JSON to XML due to its simplicity and ease of processing on the client side. XML may be great on the server side, but JSON is definitely easier to deal with on the client side.
have a look on the following url
http://www.subbu.org/blog/2006/08/json-vs-xml
Simplicity
XML is simpler than SGML, but JSON is much simpler than XML. JSON has a much smaller grammar and maps more directly onto the data structures used in modern programming languages.
Extensibility
JSON is not extensible because it does not need to be. JSON is not a document markup language, so it is not necessary to define new tags or attributes to represent data in it.
Interoperability
JSON has the same interoperability potential as XML.
Openness
JSON is at least as open as XML, perhaps more so because it is not in the center of corporate/political standardization struggles.
XML is human readable
JSON is much easier for human to read than XML. It is easier to write, too. It is also easier for machines to read and write.
XML vs JSON
JSON is better then XML that's it.
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.