JSON格式传输数据

JSON数据格式

 [编号:56,标题:“XXXXX”,timelength:90},{ID:16,标题个:“xbbx”,timelength:20}]

/ ** 
       *获取最新的视频资讯
       * @返回
       * @       * / 
      的 静态列表<News> getJSONLastNews()抛出异常,
  
抛出异常
 7 8          路径String =“http://192.168.0.168: 8080/web/ListServlet格式= json的“ 9          网址URL = 新的URL(路径);
          HttpURLConnection类康恩= (HttpURLConnection类)
== 200 ){
              的InputStream inStream中conn.getInputStream();
 返回parseJSON(inStream中);
         }
 返回空;
     }
 / ** 20      *解析JSON数据
      * @参数inStream中
      * @返回23 * / 24个私人静态清单新闻中心parseJSON的InputStream inStream中抛出异常{
 26          列表<News> newses = 新的ArrayList <News> ();
 字节 []的数据StreamTool.read(inStream中)
          字符串JSON = 新字符串(data);
          JSONArray阵列= 新JSONArray(json的);
 31(i = 0; I <array.length(); i + + ){
 33              JSONObject的JSONObject的array.getJSONObject(I);
              新闻新闻新的新闻(jsonObject.getInt(“ID”),jsonObject.getString(“标题”),jsonObject.getInt(“timelength” ));
             newses.add(新闻);
         }
 返回newses;
      }
: 公共 类StreamTool {
      / ** 
       *读取输入流数据
       * @参数inStream中
       * @返回
       / 
      的 静态 字节的InputStream inStream中的read()抛出异常{
  
          ByteArrayOutputStream outStream = 新的ByteArrayOutputStream( );
          字节 []缓冲区= 新的 字节 [1024 ]
          整数 LEN = 0 ;
          ,((LEN = inStream.read(缓冲))!= -1 ){
              outStream.write(缓冲,0 ,LEN)
          }
          inStream.close();
的         回报outStream.toByteArray();
      }
  
 }

JSON的生成

 公共 静态 无效的 主要(字串[]参数)
    {
         
          尝试
        {
            字符串JSON = “{\”名称\“:\”锐志\“}” ;
              JSONObject的jsonObj = 新 的JSONObject(json的);
              字符串名称= jsonObj.getString( “ 名称” );
 
              jsonObj.put( “ 初始” ,name.substring(0 ,1 )。toUpperCase());
 
              地图<String,字符串>成分= 新 的HashMap <String,字符串>();
              ingredients.put( “ 苹果” ,“3公斤” );
              ingredients.put( “ 糖” ,“1千克” );
              ingredients.put( “ 点心” ,“2.4千克” );
              ingredients.put(的“bestEaten” ,“室外” );
              jsonObj.put( “ 配料” ,配料);
              (jsonObj),;
        }
        赶上 (JSONExceptionË)
        {
            e.printStackTrace();
        }
 
/ / {“成分”:{“苹果”:“3公斤”,“糕点”:“2.4公斤”,“bestEaten”:“户外”,“糖”:“1千克”},
/ /“初始”,“R”,
/ /“名称”:“锐志”}
    }
JSON示例
  {
      “名字”:“微博开放平台” ,
      “域”:“OpenAPI的” ,
      “geo_enabled”:真实,
      “followers_count”:13247 ,
      “statuses_count”:158 ,
      “favourites_count “:0 ,
      “城市”:“8” ,
      “说明”:     :真正的,
      “状态” : 
     {
          “created_at”:“周一11月29十六点08分43秒+0800 2010” ,
          “文本”:“各位开发者,我们的论坛上线啦〜http://sinaurl .cn/h4FWc7 这个官方技术支持账号哦〜感谢大家对开放平台的支持〜[呵呵]“ ,
          “截断” 假17          “in_reply_to_status_id”:“” ,
          “in_reply_to_screen_name”:“” ,         “
地理” 19:空,
          “加入到最爱”:假          “in_reply_to_user_id”:“” ,
          “ID”:3958728723 ,
          “源” ,
 “的<a href = \”http://t.sina.com.cn \“= \ “nofollow的\”>新浪微博</ A>“
     },
      “ID”:11051 ,
      “性别”,“M” ,
      “friends_count”:5 ,
      “screen_name来”:“微博开放平台” 29      “allow_all_act_msg”:真正的,
 31 }
 33 34 35 JSONObject的JSONObject的新的JSONObject(JSON);
字符串名称= jsonObject.getString(“名称” );
 JSONObject的statusObject = jsonObject.getJSONObject(“状态” );
字符串创建statusObject.getString(“created_at”);
posted @ 2012-09-26 16:17  gepen  阅读(264)  评论(0编辑  收藏  举报