• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

json parsing from external source

 
John ortega
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello , ive been at my wits end with this. I cant for the life of figure out how to parse a json. Ive been at this for 2 days looking at online tutorials and vdieos but theyve all just confused me even more. My app is supposed to pull a token from heroku and im supposed to take this data and display it into a view. now ive already connected to the rest api and recieved the token. So i do have the data , i just dont know how to parse it so i can display it in a list view.

so here is were i connect to the api and take out the data. this is just a snippet of the code that shows only the part were i get the info . this part works and i successfully obtain the data i need.


i tried to follow a tutorial and use this to parse it , but i get an error that says it cannot parse this info



i must have went through 12 hour of trying to get it to work lol , hopefully somone can help me understand how to do this.

this is the json data im working with
{
"id": 1,
"scientific_name": "Platanus racemosa",
"name": "California Sycamore",
"description": "typically in river areas, but planted all throughout L.A",
"type": 1
},
{
"id": 2,
"scientific_name": "Pyrus kawakamii",
"name": "Ornamental Pear",
"description": "native to Asia, commonly planted in L.A",
"type": 1
},
{
"id": 3,
"scientific_name": "Liquidambar styraciflua",
"name": "American Sweetgum",
"description": "native to SE U.S, planted all around L.A",
"type": 1
},
{
"id": 4,
"scientific_name": "Setophaga coronata",
"name": "Yellow-rumped Warbler",
"description": "native bird, spends the winter in L.A before migrating north during the summer to breed",
"type": 2
},
{
"id": 5,
"scientific_name": "Calypte anna",
"name": "Anna's Hummingbird",
"description": "native bird, does not migrate. Spends the year in L.A",
"type": 2
},
{
"id": 6,
"scientific_name": "Regulus calendula",
"name": "Ruby-crowned Kinglet",
"description": "native bird, spends the winter in L.A before migrating north during the summer to breed",
"type": 2
}
]
 
Tim Moores
Bartender
Posts: 3127
50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seeing the full error message and stack trace would help.

Is the missing starting square bracket just a copy/paste mistake?

Also, the data doesn't have the "species" and "species2" objects that the code seems to assume it does.
 
John ortega
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Moores wrote:Seeing the full error message and stack trace would help.

Is the missing starting square bracket just a copy/paste mistake?

my question is more along the lines of how to do it , the code for the parsing i linked was just a failed attempt lol. im pretty sure its all wrong. i was hoping maybe somone has a more clear example or method for doin the parsing. I just started using android studio and im not sure how to find the errors , but i think this is it

01-19 05:45:50.803 1831-1831/com.example.romsm.lap E/ViewRootImpl: sendUserActionEvent() mView == null
01-19 05:45:51.103 1831-1867/com.example.romsm.lap D/mycode: species status 200
01-19 05:45:51.113 1831-1867/com.example.romsm.lap W/System.err: org.json.JSONException: Value [{"id":1,"type":1,"scientific_name":"Platanus racemosa","description":"typically in river areas, but planted all throughout L.A","name":"California Sycamore"},{"id":2,"type":1,"scientific_name":"Pyrus kawakamii","description":"native to Asia, commonly planted in L.A","name":"Ornamental Pear"},{"id":3,"type":1,"scientific_name":"Liquidambar styraciflua","description":"native to SE U.S, planted all around L.A","name":"American Sweetgum"},{"id":4,"type":2,"scientific_name":"Setophaga coronata","description":"native bird, spends the winter in L.A before migrating north during the summer to breed","name":"Yellow-rumped Warbler"},{"id":5,"type":2,"scientific_name":"Calypte anna","description":"native bird, does not migrate. Spends the year in L.A","name":"Anna's Hummingbird"},{"id":6,"type":2,"scientific_name":"Regulus calendula","description":"native bird, spends the winter in L.A before migrating north during the summer to breed","name":"Ruby-crowned Kinglet"}] of type org.json.JSONArray cannot be converted to JSONObject
01-19 05:45:51.113 1831-1867/com.example.romsm.lap W/System.err: at org.json.JSON.typeMismatch(JSON.java:111)
01-19 05:45:51.113 1831-1867/com.example.romsm.lap W/System.err: at org.json.JSONObject.<init>(JSONObject.java:158)
01-19 05:45:51.113 1831-1867/com.example.romsm.lap W/System.err: at org.json.JSONObject.<init>(JSONObject.java:171)
01-19 05:45:51.113 1831-1867/com.example.romsm.lap W/System.err: at com.example.romsm.lap.SpeciesJson.makeSpeciesList(SpeciesJson.java:32)
01-19 05:45:51.113 1831-1867/com.example.romsm.lap W/System.err: at com.example.romsm.lap.TreeSpeciesListActivity.getSpeciesList(TreeSpeciesListActivity.java:83)
01-19 05:45:51.113 1831-1867/com.example.romsm.lap W/System.err: at com.example.romsm.lap.TreeSpeciesListActivity.access$000(TreeSpeciesListActivity.java:26)
01-19 05:45:51.113 1831-1867/com.example.romsm.lap W/System.err: at com.example.romsm.lap.TreeSpeciesListActivity$GetSpeciesListTask.doInBackground(TreeSpeciesListActivity.java:49)
01-19 05:45:51.113 1831-1867/com.example.romsm.lap W/System.err: at com.example.romsm.lap.TreeSpeciesListActivity$GetSpeciesListTask.doInBackground(TreeSpeciesListActivity.java:46)
01-19 05:45:51.123 1831-1867/com.example.romsm.lap W/System.err: at android.os.AsyncTask$2.call(AsyncTask.java:287)
01-19 05:45:51.123 1831-1867/com.example.romsm.lap W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:234)
01-19 05:45:51.123 1831-1867/com.example.romsm.lap W/System.err: at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
01-19 05:45:51.123 1831-1867/com.example.romsm.lap W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
01-19 05:45:51.123 1831-1867/com.example.romsm.lap W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
01-19 05:45:51.123 1831-1867/com.example.romsm.lap W/System.err: at java.lang.Thread.run(Thread.java:841)

line 32 is
line 26 is , which is weird. it from my main activity public class TreeSpeciesListActivity extends AppCompatActivity {



also were is the missing square backet? i dont see it
 
sal jefferson
Greenhorn
Posts: 17
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is a sample of how I parsed JSON from the Google Books API,



I am sorry but I am not familiar enough with all this to explain everything going on here. But "rootNode.findValue("authors") returns a JSON array, so I called "authors.get(0).textValue();" to get the first index in the array.
 
Tim Moores
Bartender
Posts: 3127
50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The important part is "of type org.json.JSONArray cannot be converted to JSONObject". You need to declare "data" as JSONArray, and take it from there.

You can find the relevant javadocs at http://www.json.org/javadoc/
 
Muhammad Saifuddin
Ranch Hand
Posts: 1324
Android Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi John,

Since you're now already aware for parsing JSON data, I recommend to bypass this manual approach and do this process automated by using https://github.com/google/gson library.

You'll find many example available over the web by single hit. I just picked one found pretty managed an easy http://kylewbanks.com/blog/Tutorial-Android-Parsing-JSON-with-GSON


Hope it helps.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic