移动开发与后台数据的交互至关重要,开始:
demo运行效果:
1. JSON数据的组装
示例:
{
“name”:”xxx”,
“gender”:”男”,
“age”:”15”,
“grilFriend”:[“小美”,”小花”],
“like”:{
“eat”:”banana”,”look”:”running man”
},
“boy”:[{“女神”:”美女”,”偶像”:”梅西”},{“弟弟”:”小逗逼”,”哥哥”:”大逗逼”}]
}
代码:
JSONObject jsonObject = new JSONObject();
try {
//姓名
jsonObject.put("name", "蜘蛛人");
//性别
jsonObject.put("gender", "男");
//年龄
jsonObject.put("age", 32);
//朋友(数组形式)
JSONArray gril = new JSONArray();
gril.put("小花");
gril.put("小美");
jsonObject.put("grilFriend", gril);
//爱好
JSONObject like = new JSONObject();
like.put("eat", "banana");
like.put("look", "running man");
jsonObject.put("like", like);
//json中的object
JSONArray boy = new JSONArray();
JSONObject childer = new JSONObject();
childer.put("偶像", "梅西");
childer.put("女神", "美女");
boy.put(0, childer);
JSONObject brother = new JSONObject();
brother.put("哥哥", "大逗逼");
brother.put("弟弟", "小逗逼");
boy.put(1, brother);
jsonObject.put("boy", boy);
JSON数据的解析
解析上面自己组装的JSON文本:
JSONObject jsonObject = JsonData.createJson();//获取json文本
jsonObject.getString(“name”);
jsonObject.getString(“gender”);
jsonObject.getInt(“age”);
jsonObject.getString(“grilFriend”);
JSONArray grilArray = jsonObject.getJSONArray(“grilFriend”);
jsonObject.getString(“like”);
JSONObject likeObject = jsonObject.optJSONObject(“like”);
likeObject.getString(“look”);
likeObject.getString(“eat”);
JSONArray boyArray = jsonObject.getJSONArray("boy");
JSONObject boyObject = boyArray.optJSONObject(0);
String star = boyObject.getString("偶像");
String goddess = boyObject.getString("女神");
JSONObject brotherObject = boyArray.optJSONObject(1);
String oldBrother = brotherObject.getString("哥哥");
全部代码:
JSON文本:JsonData.java
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
/**
* Created by czc.
*/
public class JsonData {
public static JSONObject createJson() {
JSONObject jsonObject = new JSONObject();
try {
//姓名
jsonObject.put("name", "蜘蛛人");
//性别
jsonObject.put("gender", "男");
//年龄
jsonObject.put("age", 32);
//朋友(数组形式)
JSONArray gril = new JSONArray();
gril.put("小花");
gril.put("小美");
jsonObject.put("grilFriend", gril);
//爱好
JSONObject like = new JSONObject();
like.put("eat", "banana");
like.put("look", "running man");
jsonObject.put("like", like);
JSONArray boy = new JSONArray();
JSONObject childer = new JSONObject();
childer.put("偶像", "梅西");
childer.put("女神", "美女");
boy.put(0, childer);
JSONObject brother = new JSONObject();
brother.put("哥哥", "大逗逼");
brother.put("弟弟", "小逗逼");
boy.put(1, brother);
jsonObject.put("boy", boy);
} catch (JSONException e) {
e.printStackTrace();
}
return jsonObject;
}
}
MainActivity.java:
import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.w3c.dom.Text;
import java.util.ArrayList;
public class MainActivity extends Activity implements View.OnClickListener {
private Button btn_get;
private Button btn_getBranch;
private TextView tv_branch1, tv_branch2, tv_branch3, tv_branch4, tv_branch5, tv_branch6,
tv_branch7, tv_branch8, tv_branch9, tv_branch10, tv_branch11, tv_brance12;
private TextView totalData;
private String totalDataMsg;//总消息
private JSONObject jsonObject;//获取到的json文本
private String brance5;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
jsonObject = JsonData.createJson();//获取json文本
totalDataMsg = jsonObject.toString();//显示json文本
totalData = (TextView) findViewById(R.id.total);
tv_branch1 = (TextView) findViewById(R.id.branch1);
tv_branch2 = (TextView) findViewById(R.id.branch2);
tv_branch3 = (TextView) findViewById(R.id.branch3);
tv_branch4 = (TextView) findViewById(R.id.branch4);
tv_branch5 = (TextView) findViewById(R.id.branch5);
tv_branch6 = (TextView) findViewById(R.id.branch6);
tv_branch7 = (TextView) findViewById(R.id.branch7);
tv_branch8 = (TextView) findViewById(R.id.branch8);
tv_branch9 = (TextView) findViewById(R.id.branch9);
tv_branch10 = (TextView) findViewById(R.id.branch10);
tv_branch11 = (TextView) findViewById(R.id.branch11);
tv_brance12 = (TextView) findViewById(R.id.branch12);
btn_get = (Button) findViewById(R.id.btn_get);
btn_getBranch = (Button) findViewById(R.id.btn_getBrance);
btn_get.setOnClickListener(this);
btn_getBranch.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_get://总消息
totalData.setText(totalDataMsg);
break;
case R.id.btn_getBrance://得到详细的信息
try {
String name = jsonObject.getString("name");//得到json文本中name对应字段
tv_branch1.setText(name);//设置显示名字
String gender = jsonObject.getString("gender");//得到json文本中gender对应字段
tv_branch2.setText(gender);//设置显示性别
int age = jsonObject.getInt("age");//得到json文本中age对应字段
tv_branch3.setText(String.valueOf(age));//设置显示年龄
String grilFriend = jsonObject.getString("grilFriend");//得到json文本中grilFriend对应字
JSONArray grilArray = jsonObject.getJSONArray("grilFriend");//得到grilFriend对应的数组
tv_branch4.setText(grilFriend);
//将数组中的内容分别显示出来
StringBuilder sb = new StringBuilder();
for (int i = 0; i < grilArray.length(); i++) {
sb.append(grilArray.getString(i));
sb.append("、");
}
sb.deleteCharAt(sb.lastIndexOf("、"));
tv_branch5.setText(sb);
String like = jsonObject.getString("like");//得到json文本like对应的字段
JSONObject likeObject = jsonObject.optJSONObject("like");//得到like对应的object格式
String look = likeObject.getString("look");//like对应字段中,look对应的字段
String eat = likeObject.getString("eat");//like对应字段中,eat对应的字段
tv_branch6.setText(like);
tv_branch7.setText(look);
tv_branch8.setText(eat);
//JSON中的JSONObject
JSONArray boyArray = jsonObject.getJSONArray("boy");//得到json文本中boy对应的数组
tv_branch9.setText(boyArray.toString());//显示数组所有内容
JSONObject boyObject = boyArray.optJSONObject(0);//得到数组中第一段object数据
String star = boyObject.getString("偶像");//设置并显示第一段数据中,"偶像"对应的字段
tv_branch10.setText(star);
String goddess = boyObject.getString("女神");//设置并显示第一段数据中,"女神"对应的字段
tv_branch11.setText(goddess);
JSONObject brotherObject = boyArray.optJSONObject(1);//得到数组中第二段object数据
String oldBrother = brotherObject.getString("哥哥");
tv_brance12.setText(oldBrother);
} catch (JSONException e) {
e.printStackTrace();
}
break;
}
}
}
布局:activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/btn_get"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="点击获取"/>
<TextView
android:id="@+id/total"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/btn_getBrance"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="点击获取分支"/>
<TextView
android:id="@+id/branch1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/branch2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/branch3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/branch4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/branch5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/branch6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/branch7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/branch8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/branch9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/branch10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/branch11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/branch12"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
LinearLayout>