添加库文件
在App的Dependences选项卡里,单击 “+” 选择Library dependency
1,若使用JSON中的数据,就需要将JSON数据解析出来。Android 上有两种解析技术可供选择,一种是通过Android 内置的org.json包,一种是通过Google的开源Gson库(需要使用gson.jar包)。
本次讲的是第一种使用org.json解析JSON数据的方法:
格式一:
{
“name”:“tom”,
“age”:18,
“sex”:“man”
}格式二:
[18,20,30]
解析格式一:
JSONObject jsonObject = new JSONObject(data_person);
String name = jsonObject.optString("name");
String age = jsonObject.optString("age");
String sex = jsonObject.optString("sex");
解析格式二:
JSONArray jsonArray = new JSONArray(data_age);
for (int i = 0; i < jsonArray.length(); i++) {
int age=jsonArray.optInt(i);
}
做了个小demo总结了一下
json源文件:
[18,20,30]{
“name”:“tom”,
“age”:18,
“sex”:“man”
}[
{“temp”:“20’C”,“weather”:“晴天”,“name”:“北京”,“pm”:“80”,“wind”:“1级”},
{“temp”:“19’C”,“weather”:“多云”,“name”:“上海”,“pm”:“70”,“wind”:“2级”},
{“temp”:“18’C”,“weather”:“阴天”,“name”:“广州”,“pm”:“60”,“wind”:“3级”}
]
1,在raw目录下做三个.json文件存放我要解析的数据源
2,在MainActivity文件下用三个方法分别解析json文件
3,将处理后的数据set到edittext中
MainActivity代码:
package com.example.chapter_four;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.io.InputStream;
public class MainActivity extends AppCompatActivity {
EditText editText_age, editText_person, editText_weather;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText_age = (EditText) findViewById(R.id.edit_age);
editText_person = (EditText) findViewById(R.id.edit_people);
editText_weather = (EditText) findViewById(R.id.edit_weather);
age();
personl();
weather();
}
private void age() {
StringBuilder stringBuilder = new StringBuilder();
InputStream inputStream1 = getResources().openRawResource(R.raw.age);
try {
byte[] bytes = new byte[inputStream1.available()];//存
inputStream1.read(bytes);
String data_age = new String(bytes);//转
JSONArray jsonArray = new JSONArray(data_age);//转
for (int i = 0; i < jsonArray.length(); i++) {
stringBuilder.append(jsonArray.optInt(i) + "\n");
}
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
editText_age.setText(stringBuilder);
}
private void personl() {
StringBuilder stringBuilder = new StringBuilder();
InputStream inputStream2 = getResources().openRawResource(R.raw.person);
try {
byte[] bytes = new byte[inputStream2.available()];//存
inputStream2.read(bytes);
String data_person = new String(bytes);//转
JSONObject jsonObject = new JSONObject(data_person);//转
String name = jsonObject.optString("name");
String age = jsonObject.optString("age");
String sex = jsonObject.optString("sex");
stringBuilder.append(name).append(age).append(sex);
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
editText_person.setText(stringBuilder);
}
private void weather() {
StringBuilder stringBuilder = new StringBuilder();
InputStream inputStream3 = getResources().openRawResource(R.raw.weather);
try {
byte[] bytes = new byte[inputStream3.available()];//存
inputStream3.read(bytes);
String data_weather = new String(bytes);//转
JSONArray jsonArray = new JSONArray(data_weather);//转
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.optJSONObject(i);//出{...}
stringBuilder.append(jsonObject.optString("name") + "\t" + jsonObject.optString("weather") + "\t" +
jsonObject.optString("temp") + "\t" + jsonObject.optString("pm") +
"\t" + jsonObject.optString("wind") + "\n");//出“value”
}
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
editText_weather.setText(stringBuilder);
}
}
布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/tv_cc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="年龄:"
android:textSize="24dp" />
<EditText
android:id="@+id/edit_age"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="个人信息:"
android:textSize="24dp" />
<EditText
android:id="@+id/edit_people"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="天气:"
android:textSize="24dp" />
<EditText
android:id="@+id/edit_weather"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>