trouble with data from a JSON file - android

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

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.

I don't know how to classify JSON data in 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.

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()

Display of json parser in tabular form in android app

I am new to Android and I got an assignment where I have to use JSON parser to parse the link and display the content in tabular form and its table field. When clicked, it should open some sections. Please, if anyone knows any kind of tutorial on how to do it, it would be really great. Thank you.
You can use gson to parse the incoming data into POJO. It will be as simple as writing a class that matches your JSON data format.
or just just the GSON framework from google
you will need to create a Pojo(or Bean) with basic constructor and fileds matching the name of the json String.
something like:
Gson gson = new Gson();
gson.fromJson(jsonProvider, new TypeToken<Contact>(){
}.getType()
);
in this case the POJO is named Contact. After that u have an object filled with ur json data that u can use to do what ever u want...
hope this helps

how to extract web text in android like rss to display it on android GUI?

how to extract some criteria from this page http://www.zigwheels.com/api/zigtvApi.php?method=data&module=News&section=News
and filter this ( content_id , thumbnail, summary , headline , image) to display them as rss feeds in my android GUI
The output from that URL looks like a JSON feed. You can easily parse JSON data in Android using the JsonObject - see this tutorial for a comprehensive example.
A better (and probably easier) solution would be to use Google Gson and extract the object that way. I've written a full (compilable) program you can use as an example here.
You are receiving a JSONArray from that page. You should create some objects from that JSONArray which will be later displayed in a ListView. For retrieving objects from that feed you could use Json or Gson as #Marvin Pinto suggested, or you could also have a look at my ObjectFactory project, which is a very simple and easy to use parser. It can simply create your objects from a json or xml feed, and you could also use it asynchronously.
After you fetch your objects, with your desired option, you can create a ListAdapter and use it to display your objects in your ListView.

Categories

Resources