6.10

所花时间(包括上课):2.5

打码量(行):300

博客量(篇):1

了解到知识点:学习JSON

 

 package com.example.myapp;

 

import android.os.Bundle;

import android.util.Log;

 

import androidx.appcompat.app.AppCompatActivity;

 

import org.json.JSONArray;

import org.json.JSONException;

import org.json.JSONObject;

 

public class MainActivity extends AppCompatActivity {

 

    private static final String TAG = "MainActivity";

 

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

 

        // 示例代码:解析 JSON 字符串

        String jsonString = "{\"name\": \"John\", \"age\": 30, \"city\": \"New York\"}";

 

        try {

            JSONObject jsonObject = new JSONObject(jsonString);

            String name = jsonObject.getString("name");

            int age = jsonObject.getInt("age");

            String city = jsonObject.getString("city");

 

            Log.d(TAG, "Name: " + name);

            Log.d(TAG, "Age: " + age);

            Log.d(TAG, "City: " + city);

 

        } catch (JSONException e) {

            e.printStackTrace();

        }

    }

}

posted @ 2024-06-10 18:39  赵千万  阅读(4)  评论(0编辑  收藏  举报