• 复杂JSON字符串解析,可以少走弯路


    发现一个好文章:装载至http://www.verejava.com/?id=17174254038220

    package com.json5;
      
    import org.json.JSONArray;
    import org.json.JSONException;
    import org.json.JSONObject;
      
    public class Test
    {
      public static void main(String[] args)
      {
        /*
         1.将下面的JSON字符串 解析并打印出来
            {name:'李俊',age:25,address:{description:'北京 回龙观 新龙城',floor:10},like:['唱歌','画画','旅游']}
         */
        String str="{name:'李俊',age:25,address:{description:'北京 回龙观 新龙城',floor:10},like:['唱歌','画画','旅游']}";
        //JSONObject 解析
        try
        {
          JSONObject jsonObj=new JSONObject(str);
          String name=jsonObj.getString("name");
          int age=jsonObj.getInt("age");
          System.out.println(name+","+age);
            
          //地址是  JSONObject
          JSONObject addressObj=jsonObj.getJSONObject("address");
          String description=addressObj.getString("description");
          int floor=addressObj.getInt("floor");
          System.out.println(description+","+floor);
            
          //爱好是 JSONArray
          JSONArray likeArray=jsonObj.getJSONArray("like");
          for(int i=0;i<likeArray.length();i++)
          {
            String value=likeArray.getString(i);
            System.out.println(value);
          }
            
        } catch (JSONException e)
        {
          e.printStackTrace();
        }
      }
    }

  • 相关阅读:
    LVDS汇总
    smtp模块+Gmail搭建邮件发送功能
    网站建立(杂)
    xampp部署elgg
    转xampp 访问出现New XAMPP security concept
    FPGA参考电源
    android开发环境搭建问题
    cadence仿真前电路板设置(转)
    cadence串扰仿真 (转)
    cadence约束管理器总体设置 (转)
  • 原文地址:https://www.cnblogs.com/youxiu326/p/10540781.html
Copyright © 2020-2023  润新知