从JSON Android studio获取字符串

时间:2017-07-15 12:22:31

标签: android json

所以我现在遇到了问题。我想要做的就是在json文件中获取字符串的值。所以这里实际上是我的api代码。

 String accountnumber = account_no.getText().toString().trim();
    String accountname = account_name.getText().toString().trim();
    OkHttpClient client = new OkHttpClient();

    Request request = new Request.Builder()
            .url("https://api-uat.unionbankph.com/uhac/sandbox/accounts/"+accountnumber)
            .get()
            .addHeader("x-ibm-client-id", "c6243a29-8d78-40ac-9940-5adce3f3cf15")
            .addHeader("x-ibm-client-secret", "*********************")
            .addHeader("accept", "application/json")
            .build();

    Response response = client.newCall(request).execute();

我的问题实际上是从api获取数据接下来要做什么?来自api的测试这是json结果

[
{
    "account_no": "1xxxxxxxxxxxx",
    "currency": "PHP",
    "account_name": "Richard Real",
    "status": "ACTIVE",
    "avaiable_balance": "100000.00",
    "current_balance": "100000.00"
}
]

2 个答案:

答案 0 :(得分:1)

首先,您需要学习。

http://www.androidhive.info/2012/01/android-json-parsing-tutorial/

第二解析代码为。

JSONArray j_array = new JSONArray(response); // main array(root)
int len = j_array.length();
JSONObject obj_1;
for (int a = 0; a < len; a++) {
   obj_1 = j_array.getJSONObject(a); // Object on index a
   // get the data from Json Object like
   String account_no = obj_1.getString("account_no");
..... etc.
}

答案 1 :(得分:0)

试试这种方式

JSONArray jsonarray = new JSONArray(yourreposnse);

for(int i=0;i<jsonarray.lenght;i++){
JSONObject jsonobject = jsonarray.getJSONObject(i);
String account_no = jsonobject.getString("account_no");
String currency = jsonobject.getString("currency");
String account_name = jsonobject.getString("account_name");
String status = jsonobject.getString("status");
String avaiable_balance= jsonobject.getString("avaiable_balance");
String current_balance= jsonobject.getString("current_balance");
}