Android learning (27) Json file analysis

Json is a lightweight data exchange format that is both readable and easy to write.

1. Create a new Data.java to simulate a Json data

public class Data {
    / / Simulate a json data
    public  static final String jsonStr="{'arr':[1,2,3,4,5],'dat':[liu,xing,yu]}";
}

2. Parse in Activity

public class ReadJsonActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_read_json);

        System.out.println(Data.jsonStr);

        / / Analysis
        try {
            JSONObject jsonObject = new JSONObject(Data.jsonStr);
            JSONArray jsonArray = jsonObject.getJSONArray("arr");
            System.out.println(jsonArray);
            for (int i = 0; i < jsonArray.length(); i++){
                System.out.println(jsonArray.get(i));
            }
            JSONArray jsonArray2 = jsonObject.getJSONArray("dat");
            System.out.println(jsonArray2);
            for (int i = 0; i < jsonArray2.length(); i++){
                System.out.println(jsonArray2.get(i));
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        //Create Json data yourselfJSONObjectobject = new JSONObject();
        try {
            object.put("name","zs");
            object.put("age","12");
            System.out.println(object);
        }catch (JSONException e){
            e.printStackTrace();
        }
    }
}

这里写图片描述

statement:
1. Knowledge points come from "Netease Cloud Classroom" - "Android Basic Video Tutorial"
2. This article is only for my own learning record. If there is any infringement, please notify me immediately to change or delete.