发布于2021-06-14 10:53 阅读(456) 评论(0) 点赞(6) 收藏(2)
JSON是近几年才流行的一种新的数据格式,它与XML非常相似,都是用来存储数据的。但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>
(初始化数据,并添加事件处理)
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黑洞网
任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任
昵称:
评论内容:(最多支持255个字符)
---无人问津也好,技不如人也罢,你都要试着安静下来,去做自己该做的事,而不是让内心的烦躁、焦虑,坏掉你本来就不多的热情和定力
Copyright © 2018-2021 java黑洞网 All Rights Reserved 版权所有,并保留所有权利。京ICP备18063182号-2
投诉与举报,广告合作请联系vgs_info@163.com或QQ3083709327
免责声明:网站文章均由用户上传,仅供读者学习交流使用,禁止用做商业用途。若文章涉及色情,反动,侵权等违法信息,请向我们举报,一经核实我们会立即删除!