6.11

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

打码量(行):80

博客量(篇):1

了解到知识点:完善程序

 

 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-11 14:32  赵千万  阅读(1)  评论(0编辑  收藏  举报