zoukankan      html  css  js  c++  java
  • JS 的map和array集合组合返回JSON字符串

    使用map 和array 返回自定义对象的JSON字符串:

    function getObjectJSON() {
      var array = new Array();
      for (var i = 0; i < 5; i++) {
        var map = {};
        map[1] = "张三";
        map[2] = "李四";
        map[3] = "王五";
        map[4] = "赵六";
        array[i] = map;
      }
      return JSON.stringify(array);
    }
    结果:
    [{"1":"张三","2":"李四","3":"王五","4":"赵六"},{"1":"张三","2":"李四","3":"王五","4":"赵六"},{"1":"张三","2":"李四","3":"王五","4":"赵六"},{"1":"张三","2":"李四","3":"王五","4":"赵六"},{"1":"张三","2":"李四","3":"王五","4":"赵六"}]
     
    function getObjectJSON2() {
      var array = new Array();
      for (var i = 0; i < 5; i++) {
        var map = {};
        map["name"] = "张三";
        map["age"] = "24";
        map["height"] = "172cm";
        map["address"] = "中国";
        array[i] = map;
      }
      return JSON.stringify(array);
    }
    结果:
    [{"name":"张三","age":"24","height":"172cm","address":"中国"},{"name":"张三","age":"24","height":"172cm","address":"中国"},{"name":"张三","age":"24","height":"172cm","address":"中国"},{"name":"张三","age":"24","height":"172cm","address":"中国"},{"name":"张三","age":"24","height":"172cm","address":"中国"}]
  • 相关阅读:
    celery 大量消息的分布式系统 定时任务
    小程序开发-生活娱乐两不误
    linux安装selenium+chrome+phantomjs
    CENTOS 7 安装 TINYPROXY 代理服务器
    JVM(三) 垃圾回收时间点和垃圾收集器
    JDK12 concurrenthashmap源码阅读
    JVM(二) 对象存活判断和垃圾回收算法
    java 线程池(2)
    JVM(一)
    java 线程池(1)
  • 原文地址:https://www.cnblogs.com/yysbolg/p/7391529.html
Copyright © 2011-2022 走看看