当前位置:编程学习 > JS >>

生成JSON数据

[java] 
package com.example.testcreatejson; 
import org.json.JSONArray; 
import org.json.JSONObject; 
import android.app.Activity; 
import android.os.Bundle; 
import android.view.Menu; 
public class MainActivity extends Activity { 
// 生成的JSON数据1 
//  {    
//      "phones" : ["028-123456", "15002806555"], //JSON数组    
//      "name" : "小强", // 字符串    
//      "age" : 17, // 数值    
//      "address" : { "country" : "china", "province" : "四川" }, // JSON对象    
//      "married" : false // 布尔值    
// }    
     
// 生成的JSON数据2 
// 
//  { 
//      "api":"categories", 
//      "count":"3", 
//      "data":[ 
//          { 
//              "category_id":"1", 
//              "category_name":"ヘッドライン", 
//              "category_rgb":"FFFFFF", 
//              "category_news_count":"0" 
//          }, 
//          { 
//              "category_id":"2", 
//              "category_name":"ランキング", 
//              "category_rgb":"FFFFFF", 
//              "category_news_count":"0" 
//          }, 
 www.zzzyk.com//              "category_id":"3", 
//              "category_name":"社会", 
//              "category_rgb":"FFFFFF", 
//              "category_news_count":"113" 
//          } 
//      ] 
//  } 
     
     
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.activity_main);   
        try { 
            //生成JSON数据1 
            //首先是最外层 
            JSONObject resultJsonObject=new JSONObject(); 
            //phones是一个数组,所以创建JSONArray 
            JSONArray phonesJsonArray=new JSONArray(); 
            //JSONArray保存数据 
            phonesJsonArray.put("028-123456").put("15002806555"); 
            //最外层保存 
            resultJsonObject.put("phones", phonesJsonArray); 
            //最外层保存 
            resultJsonObject.put("name", "小强"); 
            //最外层保存 
            resultJsonObject.put("age", 17); 
            //address是一个对象,所以创建JSONObject 
            JSONObject addressJSONObject=new JSONObject(); 
            addressJSONObject.put("country", "china"); 
            addressJSONObject.put("province", "四川"); 
            //最外层保存 
            resultJsonObject.put("address", addressJSONObject); 
            //最外层保存 
            resultJsonObject.put("married", false); 
            System.out.println(" resultJsonObject.toString()="+resultJsonObject.toString()); 
            System.out.println("--------------------------------------------------"); 
             
            //解析JSON1 
            boolean married=resultJsonObject.getBoolean("married"); 
            JSONObject address=resultJsonObject.getJSONObject("address"); 
            String country=address.getString("country"); 
            String province=address.getString("province"); 
            int age=resultJsonObject.getInt("age"); 
            String name=resultJsonObject.getString("name"); 
            JSONArray phones=resultJsonObject.getJSONArray("phones"); 
            String phoneNumber1=phones.getStrin
补充:web前端 , JavaScript ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,