如何在没有JSON数组的情况下从复杂的JSON对象中获取字符串

时间:2017-10-13 12:50:31

标签: java android json

我已经了解了如何获得JSON数组。但我不知道如何到达这里是JSON文件的链接。 https://www.cryptocompare.com/api/data/coinlist

{
"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
}

符号下, FullName CoinName 这是我的代码

  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;
    }

1 个答案:

答案 0 :(得分:0)

您的JSON应该JSONArray,但您可以使用iterator

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