当前位置: 永利棋牌 > 小说 > 正文

解析json数据格式

时间:2019-10-01 13:19来源:小说
[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 { 

[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.2cto.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.getString(0); 
            String phoneNumber2=phones.getString(1); 
             
            System.out.println("married="+married+",country="+country+",province="+province+",age="+age+",name="+ 
            name+",phoneNumber1="+phoneNumber1+",phoneNumber2="+phoneNumber2); 
             
            System.out.println("--------------------------------------------------"); 
             
            //生成JSON数据2          
            JSONObject jsonObject=new JSONObject(); 
            jsonObject.put("api", "categories"); 
            jsonObject.put("count", "3"); 
            JSONArray dataJsonArray=new JSONArray(); 
             
            JSONObject dataJsonObject1=new JSONObject(); 
            dataJsonObject1.put("category_id", "1"); 
            dataJsonObject1.put("category_name", "name1"); 
            dataJsonObject1.put("category_rgb", "rgb1"); 
            dataJsonObject1.put("category_news_count", "1"); 
            dataJsonArray.put(dataJsonObject1); 
             
            JSONObject dataJsonObject2=new JSONObject(); 
            dataJsonObject2.put("category_id", "2"); 
            dataJsonObject2.put("category_name", "name2"); 
            dataJsonObject2.put("category_rgb", "rgb2"); 
            dataJsonObject2.put("category_news_count", "2"); 
            dataJsonArray.put(dataJsonObject2); 
             
            JSONObject dataJsonObject3=new JSONObject(); 
            dataJsonObject3.put("category_id", "3"); 
            dataJsonObject3.put("category_name", "name3"); 
            dataJsonObject3.put("category_rgb", "rgb3"); 
            dataJsonObject3.put("category_news_count", "3"); 
            dataJsonArray.put(dataJsonObject3); 
         
            jsonObject.put("data", dataJsonArray); 
            System.out.println("jsonObject.toString()="+jsonObject.toString()); 
            System.out.println("---------------------------------"); 
            //解析JSON2 
            String api=jsonObject.getString("api"); 
            String count=jsonObject.getString("count"); 
            JSONArray datas=jsonObject.getJSONArray("data"); 
            for (int i = 0; i < datas.length(); i++) { 
                JSONObject everyDataJSONObject=datas.getJSONObject(i); 
                String category_id=everyDataJSONObject.getString("category_id"); 
                String category_name=everyDataJSONObject.getString("category_name"); 
                String category_rgb=everyDataJSONObject.getString("category_rgb"); 
                String category_news_count=everyDataJSONObject.getString("category_news_count"); 
                System.out.println("category_id="+category_id+",category_name="+category_name+ 
                ",category_rgb="+category_rgb+",category_news_count="+category_news_count); 
                System.out.println("----------------------------------"); 
            } 
             
        } catch (Exception e) { 
            e.printStackTrace(); 
        } 
    } 
 
    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
        getMenuInflater().inflate(R.menu.activity_main, menu); 
        return true; 
    } 
     
  
   
     
     

json数据格式解析我自己分为两种;

  1. String category = "{'v-soft-list':[{typecode:'zhuanbi',gametype:'赚币',count:5},{typecode:'dongzuo',gametype:'动作',count:12},{typecode:'maoxian',gametype:'冒险',count:14},{typecode:'xiuxian',gametype:'休闲',count:61},{typecode:'zhili',gametype:'智力',count:17},{typecode:'gaoxiao',gametype:'搞笑',count:6},{typecode:'celue',gametype:'策略',count:4},{typecode:'qipai',gametype:'棋牌',count:2}]}";  
  2.           
  3.         JSONObject jsonObject = JSONObject.fromObject(category);    
  4.         JSONArray jsArray = jsonObject.getJSONArray("v-soft-list");    
  5.         for(int i=0;i<jsArray.size();i++){    
  6.            JSONObject jsObject = (JSONObject)jsArray.get(i);    
  7.            String typeCode = (String)jsObject.get("typecode");    
  8.            System.out.println(typeCode);    
  9.         }   

] package com.example.testcreatejson; import org.json.JSONArray; import org.json.JSONObject; import android.app.Activity; import android.os.Bundle; import android.view.Menu; pu...

一种是普通的,一种是带有数组形式的;

 

普通形式的:
服务器端返回的json数据格式如下:

{"userbean":{"Uid":"100196","Showname":"u75afu72c2u7684u7334u5b50","Avtar":null,"State":1}}

编辑:小说 本文来源:解析json数据格式

关键词: