I don't know how to classify JSON data in Android - android

I'm a newbie in Android. I'm making a crypto currency tracker. I'm using a Korean exchange API. But, when I read JSON data as String, it's a little different from what I found in literature.
In the red bracket, there are some data but are classified as the String like "BTC, ETH". And I learned in a book like this
String timestamp;
String payment_currency; // payment currency(USD, KRW...)
ArrayList<BidAskBithumb> bids = new ArrayList<BidAskBithumb>(); // Bids
ArrayList<BidAskBithumb> asks = new ArrayList<BidAskBithumb>(); // Asks
Every property is saved in String and Arrays. But in that JSON data, there is currency's name like BTC or ETH after payment_currency String. I don't know I have to make every class file like BTC.java or ETH.java. How can I solve this?
This is the JSON response I'm getting.
I read all the currency's data:
The original API documentation link.

I don't know I have to make every class file like BTC.java or ETH.java. How can I solve this?
No, you dont have to create new class files for them, you need to create model class according to your json file. & handle its behaviour.
You can try this to create your model.

Related

Android:Convert Json object to Strings.xml

I have thousand of strings in Json and I wanted to convert into xml(strings.xml) .Is there any short way to do that ? plugins ? or I have to do it one by one?
If you want to convert the json to xml in your app, you can use any existing library. This answer refers to one such.
If you just want to simply convert a json contents to xml, you can use any online converter. This one came first in my google search result. (But I would check the site's reliability before uploading any sensitive data)
You can read the entire JSON into a String object, and get the XML representation with Regex and .replace() method Of a String
Here is a smaple
val json = """{
"text_download_complete":"your file is downloaded"
}"""
val xml = json.replace("\\{\\s*".toRegex(), "<string name=")
.replace(":\\s*\"".toRegex(), ">")
.replace("\"\\s*\\}".toRegex(), "</string>")
Log.d("LOG_TAG", "onCreate: XML:\n$xml")
You can also read the JSON file instead of coping the entire file into a String...
I believe that won't be the optimum solution, but I think it would be a one shot task, and just wanted to be simple rather than looking at performance.

trouble with data from a JSON file

why the data that I put on my .json doesn't work on my native android app, I'm making an app that shows animals with images and info about them and I am following a youtube tutorial but the one that the dude uses it work perfectly but mine doesn't.
am I not making the right steps to put the info correctly?
whats a better way to pull info from a json?
I simply don't know.
this is my json format for tests:
https://gist.githubusercontent.com/fernandopuma/d33dffc07c9dfec267ff2029e79ad125/raw/fff8018e890d34b690bfaeda630249917dd2bc42/galeria.json
and this is the other:
https://gist.githubusercontent.com/draskomikic/372a8ca88d6d88ec2e45/raw/e95badd14bf24abc1b7a6dfdf4a8070515650eca
and in the project this is the line where it supose to show the work in the app, it is in the file of values/strings.xml
<string name="gallery_feed">https://gist.githubusercontent.com/draskomikic/372a8ca88d6d88ec2e45/raw/e95badd14bf24abc1b7a6dfdf4a8070515650eca</string>
I'll apreciate some opinions
thanks!!
Here are some thoughts:
1. Use jsonschema2pojo website to create Java classes from JSON
2. Add them to the project
3. Fix current JSON formatting, for example in galeria.json you have an extra comma in the end that is not supposed to be there
4. Add gson-2.7.jar library to the project
5. Use similar snippet to convert JSON into you Java objects
Gson gson = new Gson();
String json = you_json_string_with_lots_of_objects;
Type type = new TypeToken<List<Your_Animal_Object>>() {
}.getType();
List<Your_Animal_Object> list = gson.fromJson(json, type);
6. You will get a list of animal objects

Python json/list to dictionary cast not working

I'm currently deveolping an Android application that has Django framework as it's server side.
When i'm posting a data of a new user to my database i am POSTing a multipart request that has a user part inside.
The user for some reason is represented as a list but when i take it out of the request.data['user'] it's a str instance (Yea i dont know why...)
When i fetch that str i started working on it with json package.
I looked up on the internet (to many places..) how to convert a string in json format to a dictionary.
What i found is that when you use the json.loads command it doesn't give a dict back but a str instance :)
This is the code on my server side when i enter the create function of the ModelViewSet that handles the creation of the user.
userJson = request.data['user']
userJson = json.dumps(userJson)
userJson = json.loads(userJson)
What i tried to do is to make a string of my own in JSON format and that called the json.loads() command which gave me the dict object..
There seems to be a problem with processing the str from the http request of django rest framework for some reason or there's something else i am not seeing.
I tried the following links -
Converting JSON String to Dictionary Not List
http://docs.python-guide.org/en/latest/scenarios/json/
Didn't worked also..
Now, i tried accessing the str i got from json.loads() like a dictionary in this way.
id = userJson['id']
Now lets say maybe i passed a wrong json format to the loads function, it should have thrown an exception..
The code above (getting the id) raised an exception of 'String indices must be integer' - it doesn't convert it to dict! LOL xD
Good note worth mentioning - I'm trying to convert the json to a dictionary so i could access it like this - dictObject['id']
Well i would really appreciate every help!
Thanks :)
For some reason , when i did this commands-
userJson = request.data['user']
userJson = json.loads(userJson)
userJson = json.loads(userJson)
What i got to have inside the userJson after the second json.loads(userJson) I got the actual dict object to the userJson member.
Appearently it is a bug.
21 January - another update, I truly was doing double Json encoding on the Android application so that was the reason for double json. loads()

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.

Categories

Resources