when receiving my json from my loop it comes as one big object
how do I prevent this can someone point me in the right direction or documents.
Log....
W/System.err: org.json.JSONException: Value {"1":{"id":1,"name":"Bitcoin","symbol":"BTC","website_slug":"bitcoin","rank":1,"circulating_supply":17153487,"total_supply":17153487,"max_supply":21000000,"quotes":{"USD":{"price":6720.93,"volume_24h":4367200000,"market_cap":115287385383,"percent_change_1h":-0.09,"percent_change_24h":2.07,"percent_change_7d":3.26}},"last_updated":1531828586},"1027":{"id":1027,"name":"Ethereum","symbol":"ETH","website_slug":"ethereum","rank":2,"circulating_supply":100744923,"total_supply":100744923,"max_supply":null,"quotes":{"USD":{"price":472.777,"volume_24h":1774520000,"market_cap":47629882298,"percent_change_1h":-0.45,"percent_change_24h":0.3,"percent_change_7d":5.32}},"last_updated":1531828591},"52":{"id":52,"name":"XRP","symbol":"XRP","website_slug":"ripple","rank":3,"circulating_supply":39262444717,"total_supply":99991916481,"max_supply":100000000000,"quotes":{"USD":{"price":0.473278,"volume_24h":269811000,"market_cap":18582051311,"percent_change_1h":-0.18,"percent_change_24h":2.43,"percent_change_7d":4.17}},"last_updated":1531828571},"1831":{"id":1831,"name":"Bitcoin Cash","symbol":"BCH","website_slug":"bitcoin-cash","rank":4,"circulating_supply":17242013,"total_supply":17242013,"max_supply":21000000,"quotes":{"USD":{"price":793.963,"volume_24h":471637000,"market_cap":13689519971,"percent_change_1h":-0.47,"percent_change_24h":3.58,"percent_change_7d":12.07}},"last_updated":1531828592},"1765":{"id":1765,"name":"EOS","symbol":"EOS","website_slug":"eos","rank":5,"circulating_supply":896149492,"total_supply":900000000,"max_supply":1000000000,"quotes":{"USD":{"price":7.94815,"volume_24h":666448000,"market_cap":7122730586,"percent_change_1h":-0.29,"percent_change_24h":2.5,"percent_change_7d":6.31}},"last_updated":1531828590},"2":{"id":2,"name":"Litecoin","symbol":"LTC","website_slug":"litecoin","rank":6,"circulating_supply":57444758,"total_supply":57444758,"max_supply":84000000,"quotes":{"USD":{"price":83.7447,"volume_24h":283495000,"market_cap":4810693998,"percent_change_1h":-0.17,"percent_change_24h":1.63,"percent_change_7d":8.91}},"last_updated":1531828567},"512":{"id":512,"name":"Stellar","symbol":"XLM","website_slug":"stellar","rank":7,"circulating_supply":18766530971,"total_supply":104125061584,"max_supply":null,"quotes":{"USD":{"price":0.231804,"volume_24h":49352700,"market_cap":4350156945,"percent_change_1h":-0.94,"percent_change_24h":0.8,"percent_change_7d":17.92}},"last_updated":1531828584},"2010":{"id":2010,"name":"Cardano","symbol":"ADA","website_slug":"cardano","rank":8,"circulating_supply":25927070538,"total_supply":31112483745,"max_supply":45000000000,"quotes":{"USD":{"price":0.152682,"volume_24h":86195700,"market_cap":3958596984,"percent_change_1h":0.16,"percent_change_24h":1.2,"percent_change_7d":15.48}},"last_updated":1531828594},"1720":{"id":1720,"name":"IOTA","symbol":"MIOTA","website_slug":"iota","rank":9,"circulating_supply":2779530283,"total_supply":2779530283,"max_supply":2779530283,"quotes":{"USD":{"price":1.06608,"volume_24h":45845200,"market_cap":2963201644,"percent_change_1h":-0.49,"percent_change_24h":0.44,"percent_change_7d":8.21}},"last_updated":1531828590},"825":{"id":825,"name":"Tether","symbol":"USDT","website_slug":"tether","rank":10,"circulating_supply":2707140346,"total_supply":3080109502,"max_supply":null,"quotes":{"USD":{"price":1.00215,"volume_24h":2728850000,"market_cap":2712960697,"percent_change_1h":0.11,"percent_change_24h":0.45,"percent_change_7d":-0.16}},"last_updated":1531828588},"1958":{"id":1958,"name":"TRON","symbol":"TRX","website_slug":"tron","rank":11,"circulating_supply":65748111645,"total_supply":99000000000,"max_supply":null,"quotes":{"USD":{"price":0.0367706,"volume_24h":191259000,"market_cap":2417597514,"percent_change_1h":0.03,"percent_change_24h":1.55,"percent_change_7d":8.49}},"last_updated":1531828593},"1376":{"id":1376,"name":"NEO","symbol":"NEO","website_slug":"neo","rank":12,"circulating_supply":65000000,"total_supply":100000000,"max_supply":100000000,"quotes":{"USD":{"price":36.2949,"volume_24h":128189
here is my code for the request
public ArrayList getCoin () {
firstlist.clear();
final JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET,
Constants.URL_JSON, new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
try {
JSONArray coinArray = response.getJSONArray("data");
for (int i = 0; i < coinArray.length(); i++) {
JSONObject coinOBJ = coinArray.getJSONObject(i);
CoinMarketAPI coin = new CoinMarketAPI();
// coin.setId(coinOBJ.getString("id"));
//coin.setName(coinOBJ.getString("name"));
//coin.setSymbol(coinOBJ.getString("symbol"));
//coin.setWebsite_slug(coinOBJ.getString("website_slug"));
// coin.setRank(coinOBJ.getString("rank"));
// coin.setCirculating_supply(coinOBJ.getString("circulating_supply"));
// coin.setTotal_supply(coinOBJ.getString("total_supply"));
// coin.setQuotes(coinOBJ.getString("quotes"));
// coin.setUSD(coinOBJ.getString("USD"));
// coin.setPrice(coinOBJ.getString("price"));
Log.d(TAG, coin.getName());
firstlist.add(coin);
}
JSON
{
"data": {
"1": {
"id": 1,
"name": "Bitcoin",
"symbol": "BTC",
"website_slug": "bitcoin",
"rank": 1,
"circulating_supply": 17008162.0,
"total_supply": 17008162.0,
"max_supply": 21000000.0,
"quotes": {
"USD": {
"price": 9024.09,
"volume_24h": 8765400000.0,
"market_cap": 153483184623.0,
"percent_change_1h": -2.31,
"percent_change_24h": -4.18,
"percent_change_7d": -0.47
}
},
"last_updated": 1525137271
},
"1027": {
"id": 1027,
"name": "Ethereum",
"symbol": "ETH",
"website_slug": "ethereum",
"rank": 2,
"circulating_supply": 99151888.0,
"total_supply": 99151888.0,
"max_supply": null,
"quotes": {
"USD": {
"price": 642.399,
"volume_24h": 2871290000.0,
"market_cap": 63695073558.0,
"percent_change_1h": -3.75,
"percent_change_24h": -7.01,
"percent_change_7d": -2.32
}
},
NOTE some lines are commented out to prevent further errors.
Im think the error is to do the jsonarray response line but not to sure how to fix it. any help will be greatly appreciated.
data is object, not an array, so you can loop by iterator like this
JSONObject coinArray = response.getJSONObject("data");
Iterator<String> iter = coinArray.keys();
while (iter.hasNext()) {
String key = iter.next();
try {
Object coinOBJ = coinArray.get(key);
CoinMarketAPI coin = new CoinMarketAPI();
coin.setId(coinOBJ.getString("id"));
coin.setName(coinOBJ.getString("name"));
coin.setSymbol(coinOBJ.getString("symbol"));
coin.setWebsite_slug(coinOBJ.getString("website_slug"));
coin.setRank(Integer.parseInt(coinOBJ.getString("rank")));
coin.setCirculating_supply(coinOBJ.getString("circulating_supply"));
coin.setTotal_supply(coinOBJ.getString("total_supply"));
coin.setQuotes(coinOBJ.getString("quotes"));
coin.setUSD(coinOBJ.getString("USD"));
coin.setPrice(coinOBJ.getString("price"));
Log.d(TAG, coin.getName());
firstlist.add(coin);
} catch (JSONException e) {
// Something went wrong!
}
}
Let me explain it for you. What I did is that "data" was a JsonObject and has multiple objects Inside itself. so I converted data Into Json array by jsonObject.names(); Now I will get each object inside data through for loop.
JSONObject jsonObject = response.getJSONObject("data");
JSONArray jsonArray = jsonObject.names();
Log.e(tag,jsonArray.toString());
for(int i=0;i<jsonArray.length();i++){
JSONObject getObjectFromJsonArray=jsonObject.getJSONObject(jsonArray.getString(i));
CoinMarketAPI coin=new CoinMarketAPI();
coin.setId(getObjectFromJsonArray.getString("id"));
coin.setName(getObjectFromJsonArray.getString("name"));
coin.setSymbol(getObjectFromJsonArray.getString("symbol"));
coin.setWebsite_slug(getObjectFromJsonArray.getString("website_slug"));
coin.setRank(getObjectFromJsonArray.getString("rank"));
coin.setCirculating_supply(getObjectFromJsonArray.getString("circulating_supply"));
coin.setTotal_supply(getObjectFromJsonArray.getString("total_supply"));
JSONObject qoutes = getObjectFromJsonArray.getJSONObject("quotes");
JSONObject USD = qoutes.getJSONObject("USD");
coin.setPrice(USD.getString("price"));
firstlist.add(coin);
}
I want to send following data to server. It contains JsonArray of JsonObjects as shown below.
{
"users":[
{"user-1":{
"Name":"Amit",
"Age":"32",
"Gender":"male",
"Hobby":"Cricket"
}
"user-2":{
"Name":"Subodh",
"Age":"30",
"Gender":"male",
"Hobby":"Chess"
}
"user-3":{
"Name":"Mala",
"Age":"27",
"Gender":"female",
"Hobby":"Singing"
}
}
]
}
This is how I wrote json code for the same.
JSONObject userObject = new JSONObject();
JSONArray userArray = new JSONArray();
JSONObject user1 = new JSONObject();
try {
user1.put("Name", "Amit");
user1.put("Age", "32");
user1.put("Gender", "Male");
user1.put("Hobby", "Cricket");
} catch (JSONException e) {
e.printStackTrace();
}
JSONObject user2 = new JSONObject();
try {
user2.put("Name", "Subodh");
user2.put("Age", "30");
user2.put("Gender", "Male");
user2.put("Hobby", "Chess");
} catch (JSONException e) {
e.printStackTrace();
}
JSONObject user3 = new JSONObject();
try {
user3.put("Name", "Mala");
user3.put("Age", "27");
user3.put("Gender", "Female");
user3.put("Hobby", "Singing");
} catch (JSONException e) {
e.printStackTrace();
}
userArray.put(user1);
userArray.put(user2);
userArray.put(user3);
try {
userObject.put("user", userArray);
} catch (JSONException e) {
e.printStackTrace();
}
However I am not able to figure out how to give name to Objects (user-1, user-2 etc.) in the JsonArray. Can someone help to do that. I want to give Heading to each JsonObject in the JsonArray.
Your JSON is invalid.
Elements in JSON arrays don't have keys ("headings", as you call them). Only values in JSON objects have keys.
So, this is wrong:
{
"users": [
"user-1": {
"Name": "Amit",
"Age": "32",
"Gender": "male",
"Hobby": "Cricket"
}
]
}
While this is correct:
{
"users": {
"user-1": {
"Name": "Amit",
"Age": "32",
"Gender": "male",
"Hobby": "Cricket"
}
}
}
To get the correct JSON, simply use a JSONObject instead of a JSONArray:
JSONObject resultObject = new JSONObject();
JSONObject usersObject = new JSONObject();
JSONObject user1 = new JSONObject();
user1.put("Name", "Amit");
user1.put("Age", "32");
user1.put("Gender", "Male");
user1.put("Hobby", "Cricket");
usersObject.put("user-1", user1);
// repeat for user 2, 3, 4, ...
resultObject.put("users", usersObject);
Write the JSON object name like below code in every JSONObject creation :-
JSONObject jsonobj = new JSONObject("user1 ");
try {
jsonobj.put("Name", "Amit");
jsonobj.put("Age", "32");
jsonobj.put("Gender", "Male");
jsonobj.put("Hobby", "Cricket");
} catch (JSONException e) {
e.printStackTrace();
}
I just tried to get values that are stored in my JSON file and save it into sqlite database:
This is my JSON file:
{
"list": {
"meta": {
"count": 132,
"start": 0,
"type": "resource-list"
},
"resources": [
{
"resource": {
"classname": "Quote",
"fields": {
"date": "2017-03-16",
"price": 3.6720000000000002,
"type": "currency",
"symbol": "AED=X"
}
}
},
{
"resource": {
"classname": "Quote",
"fields": {
"date": "2017-03-16",
"price": 65.075000000000003,
"type": "currency",
"symbol": "AFN=X"
}
}
},
{
.............
}
............
I have tried like this but getting exception :
JSONObject mainObj = null;
try {
mainObj = new JSONObject(JSON);
JSONObject getSth = mainObj.getJSONObject("list");
if(mainObj != null){
JSONArray list = getSth.getJSONArray("resources");
if(list != null){
for(int i = 0; i < list.length();i++){
JSONObject elem = list.getJSONObject(i);
if(elem != null){
JSONObject prods = elem.getJSONObject("fields");
Object level = prods.get("type");
Toast.makeText(getApplicationContext(),""+level.toString(),Toast.LENGTH_LONG).show();
}
}
}
}
}catch (Exception e){
Toast.makeText(getApplicationContext(),""+e.toString(),Toast.LENGTH_LONG).show();
}
I was getting exception : no values in fields...
And pls give some suggestions that storing these values in Database table(matrotable) of(row fields) name, prize, symbol and type, I may try by making String Array and retrieving and storing the values for sqlite, is there any other easy options...
thanks
your fields objects are inside resource object so do
for(int i = 0; i < list.length();i++){
JSONObject elem = list.getJSONObject(i);
if(elem != null){
JSONObject prods = elem.getJSONObject("resource")
.getJSONObject("fields");
Object level = prods.get("type");
Toast.makeText(getApplicationContext(),""+level.toString(),Toast.LENGTH_LONG).show();
}
}
"resources": [ // resources list
{ // object i
"resource": { // fields are inside "resource" object
"classname": "Quote",
"fields": {
"date": "2017-03-16",
"price": 3.6720000000000002,
"type": "currency",
"symbol": "AED=X"
}
}
}
You are missing the resource JOSNObject parsing...
for(int i = 0; i < list.length();i++){
JSONObject elem = list.getJSONObject(i);
JSONObject resource = elem.getJSONObject("resource");
if(resource != null){
JSONObject prods = resource.getJSONObject("fields");
Object level = prods.get("type");
Toast.makeText(getApplicationContext(),""+level.toString(),Toast.LENGTH_LONG).show();
}
}
I recommend to you to use the simplest and easiest way to parse a json response to avoid this kind of issues:
1- generate your model classes by using this tool: http://www.jsonschema2pojo.org/ download and add the generated classes to your model package.
2- add this dependency to your gradle file:
compile 'com.google.code.gson:gson:2.4'
3- Call this method to parse your response:
Gson gson = new Gson();
ResponseModel responseModel = gson.fromJson(json, ResponseModel.class);
I am confused about parsing so I want to know about how to parse. Whenever I login with valid login id and password give response
{
"data": {
"status": "1",
"Full Name": [
{
"user_id": 1,
"user_name": "deepika#soms.in",
"full_name": "",
"display_name": "",
"token": "",
"photo_url": "http://clients.vfactor.in/putt2gether/images/profile/default.jpg"
}
],
"Event": [
{
"latest_event_id": "",
"format_id": ""
}
],
"msg": "Success Login"
}
}
and if invalid login id then give response
{
"Error": {
"msg": "Please Enter valid Email Address"
}
try {
JSONObject obj = new JSONObject(response);
if (obj.has("Error")) {
JSONObject objError = obj.getJSONObject("Error");
} else if (obj.has("data")) {
JSONObject objData = obj.getJSONObject("data");
}
} catch (Exception e) {
e.printStackTrace();
}
You need to parse the json answer.
There are many sources to learn how to do that:
Oracle documentation
Jackson documentation - Jackson is a library to parse json
GSon documentation - Another json parser
Let's assume response be,
String response;
try
{
JSONObject jsonObject = new JSONObject(response);
if (jsonObject.has("data"))
{
JSONObject jsonObjData = jsonObject.getJSONObject("data");
String status = jsonObject.getString("status");
JSONArray jsonArrayName = jsonObjData.getJSONArray("Full Name");
for (int i = 0; i < jsonArrayName.length(); i++) {
JSONObject jsonObj = jsonArrayName.getJSONObject(i);
Integer user_id = jsonObj.getInt("user_id");
String user_name = jsonObj.getString("user_name");
String full_name = jsonObj.getString("full_name");
String display_name = jsonObj.getString("display_name");
String photo_url = jsonObj.getString("photo_url");
}
JSONArray jsonArrayEvent = jsonObjData.getJSONArray("Event");
for (int i = 0; i < jsonArrayEvent.length(); i++) {
JSONObject jsonObj = jsonArrayEvent.getJSONObject(i);
String latest_event_id = jsonObj.getString("latest_event_id");
String format_id = jsonObj.getString("format_id");
}
String msg = jsonObject.getString("msg");
} else if (jsonObject.has("Error")) {
JSONObject jsonObjError = jsonObject.getJSONObject("Error");
String msg = jsonObjError.getString("msg");
}
} catch (Exception e) {
e.printStackTrace();
}
How do I fetch the third leaveTypeId and year from leaveInfo?
{
"statusCode":"1001",
"message":"Success",
"response":{
"leaveInfo":[
{
"leaveTypeId":1,
"year":2014
},
{
"leaveTypeId":2,
"year":2014
},
{
"leaveTypeId":3,
"year":2014,
},
{
"leaveTypeId":4,
"year":2014
},
{
"leaveTypeId":5,
"year":2014
},
{
"leaveTypeId":6,
"year":2014
}
]
}
}
I did this to achieve my need
JSONObject jobj1 = new JSONObject(result);
String statusCode = jobj1.getString("statusCode");
if (statusCode.equalsIgnoreCase("1001"))
{
String response = jobj1.getString("response");
JSONObject jobj2 = new JSONObject(response);
String leaveInfo = jobj2.getString("leaveInfo");
System.out.println("leaveInfo: "+leaveInfo);
}
I don't know how to go further as there are no key values for the arrays inside leaveInfo. Please help me in this regard.
Use this
JSONObject jobj1 = new JSONObject(result);
String statusCode = jobj1.getString("statusCode");
if (statusCode.equalsIgnoreCase("1001"))
{
String response = jobj1.getString("response");
JSONObject jobj2 = new JSONObject(response);
JSONArray ary=jobj2.getJSONArray("leaveInfo");
for(int i=0;i<ary.length;i++){
JSONObject obj=ary.getJSONObject(i);
String leaveInfo=obj.getString("leaveTypeId");
System.out.println("leaveInfo: "+leaveInfo);
}
}
I hope this will help to you :)