zoukankan      html  css  js  c++  java
  • JSONArray和JSONObject

    1.JSONArray和JSONObject是什么

      JSONObject 就是{x:x , x:x , ...}这样的结构

      JSONArray 是元素是JSONOject的数组,就是[ {x:x , x:x , ...} , {x:x , x:x , ...} , ... ...]这样的结构

    2.字符串转JSONArray和JSONObejct的互相转化

      1)字符串转JSONObejct

        JSONObject Jobj =JSONObject.parseObject(String str)

      2)字符串转JSONArray

        JSONArray Jarr =JSONArray.parseArray(String str)

    3.获取JSONObejct内的数据

      ·keyset   得到key的集

      ·getString(key) 得到key对应的value

    4.遍历

           //假设  str ==  {"total":8109 , "rows":"[{ ...},{ ...},{ ...}]"}
         JSONObject jsonObject = JSONObject.parseObject(Str); JSONArray jsonArray = (JSONArray) jsonObject.get(Key); JSONObject tempJSon; for (Object obj : jsonArray) { tempJSon = (JSONObject) obj; for (String str : tempJSon.keySet()) { //key == str //value == tempJSon.getString(str) pass } }

    备注:

    用的包都是fastjson

    import com.alibaba.fastjson.JSON;
    import com.alibaba.fastjson.JSONArray;
    import com.alibaba.fastjson.JSONObject;
  • 相关阅读:
    word-wrap和word-break
    sass编译生成的几个文件
    block settings drupal 8 configuration
    cookie 和 session
    程序员思维
    psr-0 和 psr-4
    eclipse的字符编码问题
    合并代码并上传
    如何部署代码
    301 CACHE
  • 原文地址:https://www.cnblogs.com/yejiang/p/10537508.html
Copyright © 2011-2022 走看看