教程集 www.jiaochengji.com
教程集 >  脚本编程  >  java  >  正文 java中list数组转json数组例子

java中list数组转json数组例子

发布时间:2016-10-15   编辑:jiaochengji.com
教程集为您提供java中list数组转json数组例子等资源,欢迎您收藏本站,我们将为您提供最新的java中list数组转json数组例子资源
list数组转json数组在java中有函数可以实现了我们只需要使用JSONArray对象就可以实现了,今天我们就一起来看看小编整理的几个关于list数组转json数组例子吧。

例子一

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy8444')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy8444>

package com.yq1012.fastjson;
import java.util.ArrayList;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

public class TestListTojson {
 public static void main(String[] args) {
  
  //[{"title":"中国好声音"},{title:"中国移动网上营业厅"},{title:"中国银行"},{title:"中国移动"},{title:"中国好声音第三期"},{title:"中国好声音 第一期"},{title:"中国电信网上营业厅"},{title:"中国工商银行"},{title:"中国好声音第二期"},{title:"中国地图"}]
  
  JSONArray array=new JSONArray();
  ArrayList<String> list=new ArrayList<String>();
  list.add("中国好声音");
  list.add("中国好声音1");
  list.add("中国好声音2");
  list.add("中国好声音3");
  for (String string : list) {
   JSONObject json= new JSONObject();
   json.put("title", string);
   array.add(json);
  }
  System.out.println(array.toString());
  
 }

}

</td></tr></table>

例子二

将json格式的字符数组转为List对象

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy9662')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy9662>

package hb;

import java.util.Date;

public class Person {
 
 String id;
 int age;
 String name;
 Date birthday;
 public String getId() {
  return id;
 }
 public void setId(String id) {
  this.id = id;
 }
 public int getAge() {
  return age;
 }
 public void setAge(int age) {
  this.age = age;
 }
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
 public Date getBirthday() {
  return birthday;
 }
 public void setBirthday(Date birthday) {
  this.birthday = birthday;
 }
 
}
 


package hb;

import java.util.Iterator;
import java.util.List;

import org.junit.Test;

import net.sf.json.JSONArray;
import net.sf.json.JsonConfig;

public class JsonToList {

 public static void main(String[] args) {
  String json="[{'name':'huangbiao','age':15},{'name':'liumei','age':14}]";
  JSONArray jsonarray = JSONArray.fromObject(json);
  System.out.println(jsonarray);
  List list = (List)JSONArray.toCollection(jsonarray, Person.class);
  Iterator it = list.iterator();
  while(it.hasNext()){
   Person p = (Person)it.next();
   System.out.println(p.getAge());
  }
 }
 
 @Test
 public void jsonToList1(){
  String json="[{'name':'huangbiao','age':15},{'name':'liumei','age':14}]";
  JSONArray jsonarray = JSONArray.fromObject(json);
  System.out.println(jsonarray);
  List list = (List)JSONArray.toList(jsonarray, Person.class);
  Iterator it = list.iterator();
  while(it.hasNext()){
   Person p = (Person)it.next();
   System.out.println(p.getAge());
  }
  
 }
 
 @Test
 public void jsonToList2(){
  String json="[{'name':'huangbiao','age':15},{'name':'liumei','age':14}]";
  JSONArray jsonarray = JSONArray.fromObject(json);
  System.out.println(jsonarray);
  System.out.println("------------");
  List list = (List)JSONArray.toList(jsonarray, new Person(), new JsonConfig());
  Iterator it = list.iterator();
  while(it.hasNext()){
   Person p = (Person)it.next();
   System.out.println(p.getAge());
  }
  
 }

}
 

</td></tr></table>

将list对象转为JSON字符串数组

 

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy5415')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy5415>


package hb;

import java.util.LinkedList;
import java.util.List;

import net.sf.json.JSONArray;

public class ListToJson {

 public static void main(String[] args) {
  List list = new LinkedList();
  for(int i=0;i<3;i ){
   Person p = new Person();
   p.setAge(i);
   p.setName("name" i);
   list.add(p);
  }
  JSONArray jsonarray = JSONArray.fromObject(list);
  System.out.println(jsonarray);
 }

}
 打印结果

Java代码 
1.[{"age":0,"birthday":null,"id":"","name":"name0"},{"age":1,"birthday":null,"id":"","name":"name1"},{"age":2,"birthday":null,"id":"","name":"name2"}] 

</td></tr></table>

您可能感兴趣的文章:
java中list数组转json数组例子
java将数据转成json编码格式技术实例教程
接口返回数据用xml好还是json理解
php数组与Json转换的方法探讨
js数组转化为字符串示例
jQuery数组处理方法汇总
PHP 与 js json的通信实例
java中删除 数组中的指定元素
jquery数组封装使用方法分享(jquery数组遍历)
讲解Python3内置模块之json编码解码方法

[关闭]
~ ~