讀取main/assets目錄下的text文本

text文本的json字符串,計(jì)算counter_fee的總和
{
"price_floor":0,
"price_ceil":1000000,
"data":[
{
"accident_floor":1,
"accident_ceil":2,
"loss_acquire":true,
"new_car":false,
"CLIVTA":{
"counter_fee":"0",
"risk_grade_1":"C01",
"risk_grade_2":"C23"
},
"VI":{
"counter_fee":"26",
"risk_grade_1":"C01",
"risk_grade_2":"C15"
}
},
{
"accident_floor":0,
"accident_ceil":0,
"loss_acquire":false,
"new_car":true,
"CLIVTA":{
"counter_fee":"2",
"risk_grade_1":"C01",
"risk_grade_2":"C23"
},
"VI":{
"counter_fee":"26",
"risk_grade_1":"C01",
"risk_grade_2":"C15"
}
},
{
"accident_floor":1,
"accident_ceil":2,
"loss_acquire":false,
"new_car":false,
"CLIVTA":{
"counter_fee":"10",
"risk_grade_1":"C01",
"risk_grade_2":"C23"
},
"VI":{
"counter_fee":"16",
"risk_grade_1":"C01",
"risk_grade_2":"C15"
}
}
]
}
配置文件:
compile 'com.google.code.gson:gson:2.3.1'
參考所有代碼:
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.google.gson.Gson;
import java.io.InputStream;
import java.util.List;

public class MainActivity extends AppCompatActivity {
TextView tv;
List<TestBean.DataBean> data;
private Object tatolCount;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    tv = (TextView) findViewById(R.id.tv_text);

    Button button = (Button) findViewById(R.id.button);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            gotoSecondActivity();
        }
    });
    getTextData();

}
private void gotoSecondActivity() {
    startActivity(new Intent(this,SecondActivity.class));
}
private void getTextData(){
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            try {
                //獲得AssetManger 對(duì)象, 調(diào)用其open 方法取得  
                //對(duì)應(yīng)的inputStream對(duì)象
                InputStream is = getAssets().open("test.txt");
                int size = is.available();//取得數(shù)據(jù)流的數(shù)據(jù)大小
                byte[] buffer = new byte[size];
                is.read(buffer);
                is.close();
                String txt = new String(buffer);
                tv.setText(txt);

                TestBean testBean = new Gson().fromJson(txt, TestBean.class);
                data = testBean.getData();


            } catch (Exception e) {
                Log.e("Assert", e.toString());
            }
        }
    });
    getTatolCount(data);
}

public void getTatolCount(List<TestBean.DataBean> data) {
    int count = 0;
    for (int i= 0;i< data.size();i++){
        String counter_fee = data.get(i).getCLIVTA().getCounter_fee();
        int i1 = Integer.parseInt(counter_fee);
        String counter_fee1 = data.get(i).getVI().getCounter_fee();
        int i2 = Integer.parseInt(counter_fee1);
        count += i1+i2;
    }
    Log.e("TAG","count"+count);
}

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容