package com.example.zuoye1;
import android.os.Bundle;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.io.InputStream;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView text=(TextView )findViewById(R.id.text);
try {
InputStream is = this.getResources().openRawResource(R.raw.test);
byte[] buffer = new byte[is.available()];
is.read(buffer);
String json = new String(buffer, "utf-8");
JSONObject jsonObj = new JSONObject(json);//創建json物件
String name = jsonObj.optString("name");
text.append("\n" + name + "\n");
int age = jsonObj.optInt("age");
text.append(String.valueOf(age) + "\n");
boolean married = jsonObj.optBoolean("married");
text.append(String.valueOf("married"));
}
catch(IOException e) {
e.printStackTrace();
}catch(JSONException e){
e.printStackTrace() ;
}
}
}
uj5u.com熱心網友回復:
本地有一個資源檔案R.raw.test,res的raw目錄下,讀出內容,轉為自帶的JSONObject物件,然后就是讀取節點的值了uj5u.com熱心網友回復:
是要為代碼添加一下注釋
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/74154.html
標籤:Android
