package activity.cyq.datalrearn;
import android.support.v.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
public class JsonLearn extends AppCompatActivity {
private TextView writeText, readText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_json_learn);
readText = (TextView) findViewById(R.id.readJsonText);
writeText = (TextView) findViewById(R.id.writeJsonText);
/*读取Json数据*/
findViewById(R.id.readJsioBtn).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
/*获取到assets文件下的TExt.json文件的数据,并以输出流形式返回。*/
InputStream is = JsonLearn.this.getClass().getClassLoader().getResourceAsStream("assets/" + "Text.json");
InputStreamReader streamReader = new InputStreamReader(is);
BufferedReader reader = new BufferedReader(streamReader);
String line;
StringBuilder stringBuilder = new StringBuilder();
try {
while ((line = reader.readLine()) != null) {
// stringBuilder.append(line);
stringBuilder.append(line);
}
reader.close();
reader.close();
is.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
JSONObject person = new JSONObject(stringBuilder.toString());
JSONArray infArray = person.getJSONArray("inf");
for (int i = ; i < infArray.length(); i++) {
JSONObject inf_Array = infArray.getJSONObject(i);
readText.append("name:" + inf_Array.getString("name") + "n");
readText.append("IdCard:" + inf_Array.getString("IdCard"));
readText.append("age:" + inf_Array.getInt("age"));
readText.append("married:" + inf_Array.getBoolean("married"));
}
} catch (JSONException e) {
e.printStackTrace();
}
}
});
/*创建Json数据并显示*/
findViewById(R.id.writeJsioBtn).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
JSONObject inf = new JSONObject();
inf.put("number", );
JSONArray array = new JSONArray();
JSONObject arr_ = new JSONObject();
arr_.put("name", "张三");
arr_.put("age", );
arr_.put("IdCard", "XC");
arr_.put("married", true);
JSONObject arr_ = new JSONObject();
arr_.put("name", "李四");
arr_.put("age", );
arr_.put("IdCard", "@DC");
arr_.put("married", true);
array.put(, arr_);
array.put(, arr_);
inf.put("inf", array);
writeText.setText(inf.toString());
} catch (JSONException e) {
e.printStackTrace();
}
}
});
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有