Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
380 views
in Technique[技术] by (71.8m points)

java - How can i get Strings from a complex JSON Object without a JSON Array

i have understood how to get a JSON Array. but i dont know how to get here is the link to the JSON file. https://www.cryptocompare.com/api/data/coinlist or

{
"Response": "Success",
"Message": "Coin list succesfully returned!",
"BaseImageUrl": "https://www.cryptocompare.com",
"BaseLinkUrl": "https://www.cryptocompare.com",
"DefaultWatchlist": {
    "CoinIs": "1182,7605,5038,24854,3807,3808,202330,5324,5031,178978",
    "Sponsored": "1182"
},
"Data": {
    "USC": {
        "Id": "100954",
        "Url": "/coins/usc/overview",
        "ImageUrl": "/media/1383363/usc.png",
        "Name": "USC",
        "Symbol": "USC",
        "CoinName": "Ultimate Secure Cash",
        "FullName": "Ultimate Secure Cash (USC)",
        "Algorithm": "SHA256",
        "ProofType": "PoS",
        "FullyPremined": "0",
        "TotalCoinSupply": "200084200",
        "PreMinedValue": "N/A",
        "TotalCoinsFreeFloat": "N/A",
        "SortOrder": "1233",
        "Sponsored": false
    },
    "DUX": {
        "Id": "105206",
        "Url": "/coins/dux/overview",
        "ImageUrl": "/media/1383364/dux.png",
        "Name": "DUX",
        "Symbol": "DUX",
        "CoinName": "DuxCoin",
        "FullName": "DuxCoin (DUX)",
        "Algorithm": "Scrypt",
        "ProofType": "PoW/PoS",
        "FullyPremined": "0",
        "TotalCoinSupply": "1680000000",
        "PreMinedValue": "N/A",
        "TotalCoinsFreeFloat": "N/A",
        "SortOrder": "1234",
        "Sponsored": false
    },
    "XPS": {
        "Id": "105220",
        "Url": "/coins/xps/overview",
        "ImageUrl": "/media/1383365/xps.png",
        "Name": "XPS",
        "Symbol": "XPS",
        "CoinName": "PoisonIvyCoin",
        "FullName": "PoisonIvyCoin (XPS)",
        "Algorithm": "Scrypt",
        "ProofType": "PoW/PoS",
        "FullyPremined": "0",
        "TotalCoinSupply": "4666666667",
        "PreMinedValue": "N/A",
        "TotalCoinsFreeFloat": "N/A",
        "SortOrder": "1235",
        "Sponsored": false
    },
    "EQT": {
        "Id": "105263",
        "Url": "/coins/eqt/overview",
        "ImageUrl": "/media/1383366/eqt.png",
        "Name": "EQT",
        "Symbol": "EQT",
        "CoinName": "EquiTrader",
        "FullName": "EquiTrader (EQT)",
        "Algorithm": "Scrypt",
        "ProofType": "PoW",
        "FullyPremined": "0",
        "TotalCoinSupply": "72000000",
        "PreMinedValue": "N/A",
        "TotalCoinsFreeFloat": "N/A",
        "SortOrder": "1236",
        "Sponsored": false
    },
    "INSN": {
        "Id": "105284",
        "Url": "/coins/insn/overview",
        "ImageUrl": "/media/1383366/insn.png",
        "Name": "INSN",
        "Symbol": "INSN",
        "CoinName": "Insane Coin",
        "FullName": "Insane Coin (INSN)",
        "Algorithm": "X11",
        "ProofType": "PoW/PoS",
        "FullyPremined": "0",
        "TotalCoinSupply": "30000000",
        "PreMinedValue": "N/A",
        "TotalCoinsFreeFloat": "N/A",
        "SortOrder": "1237",
        "Sponsored": false
    }
},
"Type": 100
}

Symbol, FullName and CoinName this is my code

  try {
            // Hashmap for ListView
            ArrayList<HashMap<String, String>> PriceList = new ArrayList<HashMap<String, String>>();

            JSONObject jsonObj = new JSONObject(json);





            JSONObject mJ = jsonObj.getJSONObject("Response");




            for (int i = 0; i < json.length(); i++) {
                JSONObject c = mJ.getJSONObject("Data");

                String  symbol = c.getString("Symbol");
                String  fName =  c.getString("FullName");
                String  cName =  c.getString("CoinName");


                // tmp hashmap for single data
                HashMap<String, String> data = new HashMap<>();

                // adding each child node to HashMap key => value
                data.put("Symbol",symbol);
                data.put("FullName",fName);
                data.put("CoinName",cName);

                // adding data to listView
                PriceList.add(data);
            }
            return PriceList;
        } catch (JSONException e) {
            e.printStackTrace();
            return null;
        }
    } else {
        Log.e("ServiceHandler", "Couldn't get any data from the url");
        return null;
    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Your JSON should have JSONArray but you can use iterator for this.

 try
{   
    String jsonString="";  //Use your JSON string/response here.
    JSONObject jObject= new JSONObject(jsonString).getJSONObject("Data");
    Iterator<String> keys = jObject.keys();
    while( keys.hasNext() )
    {
        String key = keys.next();

        JSONObject innerJObject = jObject.getJSONObject(key);

    }
}
catch (JSONException e)
{   e.printStackTrace();    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...