使用jsoup-1.10.1.jar创建json格式文件
生成如下格式:
{ "Phone":["123214123124","3123124132"], "married":false, "address":{"province":"bejing","country":"china"}, "age":100, "name":"yang" }
{}有大括号的是对象,使用JSONObject
[] 中括号的是数组,使用JSONArray
具体代码:
1 public class MainActivity extends AppCompatActivity{ 2 3 private TextView mTextView; 4 private Button mButton; 5 6 @Override 7 protected void onCreate ( Bundle savedInstanceState ) { 8 super.onCreate(savedInstanceState); 9 setContentView(R.layout.activity_main); 10 mTextView = (TextView)findViewById(R.id.tv_content); 11 } 12 13 //构造一个Json对象 14 public void query(View view){ 15 try{ 16 //首先最外层是{},创建一个对象 17 JSONObject person = new JSONObject(); 18 //第一个键phone的值是数组,所以需要创建数组对象 19 JSONArray phone = new JSONArray(); 20 phone.put("123214123124").put("3123124132"); 21 person.put("Phone",phone); 22 23 person.put("name","yang"); 24 person.put("age",100); 25 26 //键address的值是对象,所以要创建一个对象 27 JSONObject address = new JSONObject(); 28 address.put("country","china"); 29 address.put("province","bejing"); 30 person.put("address",address); 31 person.put("married",false); 32 System.out.println(person.toString()); 33 mTextView.setText(person.toString()); 34 } 35 catch(Exception e){ 36 e.printStackTrace(); 37 } 38 39 } 40 }