程序员最近都爱上了这个网站  程序员们快来瞅瞅吧!  it98k网:it98k.com

本站消息

站长简介/公众号

  出租广告位,需要合作请联系站长


+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

2023-06(3)

JSON数据格式(Android)

发布于2021-06-14 10:53     阅读(456)     评论(0)     点赞(6)     收藏(2)


JSON是近几年才流行的一种新的数据格式,它与XML非常相似,都是用来存储数据的。但JSON相对于XML来说,解析速度更快,占用空间更小。
下面我们直接来干货。

1、设计布局文件activity_json.xml

布局属性如下:
在这里插入图片描述

布局文件activity_json.xml源代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:layout_marginLeft="20dp"
    android:layout_marginRight="20dp"
    android:layout_marginTop="20dp"
    android:weightSum="1">
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textView"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="2dp"
        android:text="城市"
        android:textSize="24sp"/>

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/editText"
        android:inputType="textPersonName"
        android:textSize="24sp"/>
</LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textView2"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="2dp"
        android:text="气温"
        android:textSize="24sp"/>
    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/editText2"
        android:inputType="textPersonName"
        android:textSize="24sp"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/button"
            android:layout_marginTop="2dp"
            android:layout_marginLeft="20dp"
            android:text="解析单个jason数据"
            android:textSize="24sp"/>
    </LinearLayout>
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/imageView"
        app:srcCompat="@drawable/logo"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/txt3"
            android:textSize="24sp"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/button2"
            android:layout_marginTop="2dp"
            android:layout_marginLeft="20dp"
            android:text="解析单多个jason数据"
            android:textSize="24sp"/>
    </LinearLayout>


</LinearLayout>

2、设计控制文件JsonActivity.java

(初始化数据,并添加事件处理)

运行效果如下

在这里插入图片描述

控制文件JsonActivity.java源代码

package com.example.myapplication5;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class JsonActivity<TSONObject> extends AppCompatActivity implements View.OnClickListener {
    EditText txt1,txt2;
    TextView txt3;
    JSONObject p1,p2,p3;
    JSONArray weather;
    Button jsonBtn,arrayBtn;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_json);
        txt1 = (EditText)findViewById(R.id.editText);
        txt2 = (EditText)findViewById(R.id.editText2);
        txt3 = (TextView)findViewById(R.id.txt3);
        jsonBtn = (Button) findViewById(R.id.button);
        jsonBtn.setOnClickListener(this);
        arrayBtn = (Button) findViewById(R.id.button2);
        arrayBtn.setOnClickListener(this);
        try {
            p1=new JSONObject("{\"城市\":\"大理\", \"气温\":\"0-14度,多云\"}");
            p2=new JSONObject("{\"城市\":\"成都\", \"气温\":\"2-4度,小雨\"}");
            p3=new JSONObject("{\"城市\":\"拉萨\", \"气温\":\"-9-5度,多云\"}");
            weather =new JSONArray();
            weather.put(p1);
            weather.put(p2);
            weather.put(p3);
        } catch ( JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onClick(View v) {
        if(v == jsonBtn)
            setJsonData();
        else if(v == arrayBtn)
            setandgetArrayData();

    }

    void setJsonData() {
        try {
            JSONObject test= new JSONObject();
            test.put("城市","深圳");
            test.put("气温",30);
            String jc=test.getString("城市");
            txt1.setText(jc);
            int jw = test.getInt("气温");
            txt2.setText(Integer.toString(jw));
        }catch (JSONException e){  }
    }

    void setandgetArrayData() {
        try {
            String jc,jw;
            int length = weather.length();
            for(int i=0; i<length; i++){    //遍历JSONArray
                JSONObject jsonObject = weather.getJSONObject(i);
                jc = jsonObject.getString("城市") + ":";
                jw =  jsonObject.getString("气温") + "\n";
                txt3.append(jc + jw);
            }
        }catch (JSONException e){  }
    }


}

在这里插入图片描述

原文链接:https://blog.csdn.net/wkt1105436760/article/details/117756009



所属网站分类: 技术文章 > 博客

作者:快起来搬砖啦

链接:http://www.javaheidong.com/blog/article/222739/f84db0f324a129f3353c/

来源:java黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

6 0
收藏该文
已收藏

评论内容:(最多支持255个字符)