package com.qianmo.beziertest;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
import com.qianmo.beziertest.entity.UserInfo;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TooManyListenersException;
public class Main2Activity extends AppCompatActivity {
/**
*
{
"name":"王小二",
"age":25,
"birthday":"1998-10-23",
"school":"蓝翔",
"major":[
"理发",
"挖掘机"
],
"car":null
}
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
generate_JSON_by_original();
//generate_JSON_by_HashMap();
//generate_JSON_by_JavaBean();
}
private void generate_JSON_by_JavaBean() {
UserInfo userInfo = new UserInfo();
userInfo.setName("王小二");
userInfo.setAge(25);
userInfo.setBirthday("1998-10-23");
userInfo.setSchool("蓝翔");
List<String> list = new ArrayList<>();
list.add("理发");
list.add("挖掘机");
userInfo.setMajor(list);
userInfo.setCar(null);
try {
Log.d("ZhangYubin",new JSONObject(String.valueOf(userInfo)).toString());
} catch (JSONException e) {
e.printStackTrace();
}
}
private void generate_JSON_by_HashMap() {
Map<String,Object> objectHashMap = new HashMap<String, Object>();
objectHashMap.put("name","王小二");
objectHashMap.put("age",25);
objectHashMap.put("birthday","1998-10-23");
objectHashMap.put("school","蓝翔");
objectHashMap.put("major",new String[] {"理发","挖掘机"});
objectHashMap.put("car",null);
objectHashMap.put("name","李小二");
Log.d("ZhangYubin",new JSONObject(objectHashMap).toString());
}
private void generate_JSON_by_original() {
JSONObject jsonObject = new JSONObject();
try {
jsonObject.put("name","王小二");
jsonObject.put("age",25);
jsonObject.put("birthday","1998-10-23");
jsonObject.put("school","蓝翔");
//jsonObject.put("major",new String[] {"理发","挖掘机"});
//jsonObject.put("car",null);
jsonObject.put("name","李小二");
Log.d("ZhangYubin",jsonObject.toString());
} catch (JSONException e) {
e.printStackTrace();
}
Toast.makeText(Main2Activity.this, jsonObject.toString(), Toast.LENGTH_SHORT).show();
}
}
public class Main3Activity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
parse_JSON_by_file();
}
private void parse_JSON_by_file() {
StringBuffer stringBuffer = new StringBuffer();
try{
AssetManager assetManager = Main3Activity.this.getAssets();
BufferedReader bf = new BufferedReader(new InputStreamReader(assetManager.open("ss")));
String line;
while ((line = bf.readLine())!= null){
stringBuffer.append(line);
Log.e("ZhangYubin",line);
}
}catch (IOException e){
e.printStackTrace();
}
String result = stringBuffer.toString();
Log.d("ZhangYubin",result);
Toast.makeText(this, result, Toast.LENGTH_SHORT).show();
/*
try{
JSONObject object = new JSONObject(result);
String name = object.getString("name");
int age = object.getInt("age");
String birthday = object.getString("birthday");
String school = object.getString("school");
JSONArray array = object.getJSONArray("major");
for (int i = 0; i <array.length() ; i++) {
}
}catch (JSONException e){
e.printStackTrace();
}
*/
//使用gson来解析数据
Gson gson =new Gson();
UserInfo info = gson.fromJson(result,UserInfo.class);
int age = info.getAge();
String brithday = info.getBirthday();
Log.e("ZhangYubin","age is "+age+", brithday is "+brithday);
}
}
public class Main4Activity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main4);
UserInfo userInfo = new UserInfo();
userInfo.setName("王小二");
userInfo.setAge(25);
userInfo.setBirthday("1998-10-23");
userInfo.setSchool("蓝翔");
List<String> list = new ArrayList<>();
list.add("理发");
list.add("挖掘机");
userInfo.setMajor(list);
userInfo.setCar(null);
Gson gson =new Gson();
Log.e("ZhangYubin",gson.toJson(userInfo).toString());
}
}
public class UserInfo {
/**
* name : 王小二
* age : 25
* birthday : 1998-10-23
* school : 蓝翔
* major : ["理发","挖掘机"]
* car : null
*/
private String name;
private int age;
private String birthday;
private String school;
private Object car;
private List<String> major;
private transient double money;//json生成过程中忽略掉这个属性
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getBirthday() {
return birthday;
}
public void setBirthday(String birthday) {
this.birthday = birthday;
}
public String getSchool() {
return school;
}
public void setSchool(String school) {
this.school = school;
}
public Object getCar() {
return car;
}
public void setCar(Object car) {
this.car = car;
}
public List<String> getMajor() {
return major;
}
public void setMajor(List<String> major) {
this.major = major;
}
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
}