zoukankan      html  css  js  c++  java
  • 使用 json_in_java

    java_in_json

    1 Java 使用 Json

     

    1.2 构造 json 字符串

    JSONObject jsonObject = new JSONObject();
    jsonObject.put("name", "zhangsan");
    jsonObject.put("age", 11);
    
    JSONArray array = new JSONArray();
    array.put(0,"lisi");
    array.put(1,"wangwu");
    array.put(2,"zhaoliu");
    jsonObject.put("friends", array);
    
    System.out.println(jsonObject.toString());
    

    结果: {"friends":["lisi","wangwu","zhaoliu"],"age":11,"name":"zhangsan"}

    1.3 解析 json 字符串

    String str = "{"friends":["lisi","wangwu","zhaoliu"],"age":11,"name":"zhangsan"}";
    JSONTokener tokener = new JSONTokener(str);
    JSONObject object = (JSONObject) tokener.nextValue();
    
    System.out.println(object.getString("name"));
    System.out.println(object.getInt("age"));
    
    JSONArray arr = object.getJSONArray("friends");
    for (int i = 0; i < arr.length(); i++) {
            String friendName = (String) arr.get(i);
            System.out.println(friendName);
    }
    

    结果: zhangsan 11 lisi wangwu zhaoliu

    1.4 进一步使用,查看文档.

    Author: dai

    Created: 2016-04-12 Tue 17:03

    Emacs 24.5.1 (Org mode 8.2.10)

    Validate

  • 相关阅读:
    Core Foundation框架(2)命名规范,内省
    Core Foundation框架(1)基础介绍
    Swift 数组,字典
    Swift 操作符
    Swift 可选值
    Swift 元组
    Swift 变量声明
    iOS开发_UI_AutoLayout
    iOS开发_Objective-C_字符串操作
    iOS开发_Objective-C_监听搜索时用户输入的拼音
  • 原文地址:https://www.cnblogs.com/daihanlong/p/5383382.html
Copyright © 2011-2022 走看看