JSON传递数据
来源:互联网 发布:轻云4g网络下怎么用 编辑:程序博客网 时间:2024/05/21 09:59
1.JSON字符串
json指的是javascript对象表示方法,全称为Javascript Object Notation,是独立于语言的轻量级文本数据交换格式,传递速率较快于xml,是现阶段作为数据传递的主要格式。
先给出一个json字符串的示例:
[ { "age": 20, "gender": "male", "city": "nanjing", "score": [0,-1,0,-1,0,0] }, { "age": 52, "gender": "male", "city": "nanjing", "score": [-1,0,1,1,1,0] }, { "age": 39, "gender": "male", "city": "nanjing", "score": [0,0,0,1,-1,1] }, { "age": 22, "gender": "male", "city": "nanjing", "score": [1,0,1,-1,0,0] } ]
上面的json字符串不难理解,这也是json串的优点所在(xml也同样具有这样的特性),这段json串包含了4个对象,每个对象都有自己的属性即年龄(age)、性别(gender)、城市(city)以及评分(sorce)。每个对象用大括号{}所囊括,属性名称由双引号包围,属性值与属性名称用冒号链接,不同属性之间用逗号相隔,不同对象之间也用逗号相隔。
Json的语法是Javascript的子语法,也就是说这一套体系在javascript中同样适用。Json主要的作用是用于数据的传递,因此涉及到了数据收发的问题。
2.Json串的接收
我们后台程序以java为例。在java平台中,接收Json串的实质就是将数据存储为不同的对象,如上文所提到的这个Json串,我们可以创建一个JavaBean用于接收上述Json串,因此我们构造了一个User类,其中包含了age,gender,city以及score等私有属性,User类代码如下。
public class User{ private int age; private String gender; private String city; private int[] score; //Construct //Getter and Setter}
然后将前文提及的Json串写入一个.json文件并保存。利用java进行文件的读写。我们可以创建一个java的文件读取工具类FileUtil来统一存放读取文件方法,FileUtil类如下:
public class FileUtil { public String readFile(String filename){ String str=""; BufferedReader reader=null; try { FileInputStream fileInputStream=new FileInputStream(filename); InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "UTF-8"); reader=new BufferedReader(inputStreamReader); String tempStr=null; while((tempStr=reader.readLine())!=null){ str+=tempStr; } reader.close(); } catch (Exception e) { e.printStackTrace(); }finally{ if(reader!=null){ try { reader.close(); } catch (IOException e) { e.printStackTrace(); } } } return str; }}
该工具类中暂时只写了一种方法即readFile(String filename),filename是将要读取文件的绝对路径,函数返回一个由文件内容构成的字符串。这个字符串也就是我们需要的json串,通过对这个字符串的解析,我们便可以得到相应的实体类,这里我们用到了json-lib.jar包,具体代码如下
public class JsonTest { public static void main(String[] args) { FileUtil fileUtil=new FileUtil(); JsonUtil jsonUtil=new JsonUtil(); List<User> userList=new ArrayList<User>(); String filename="D:/workspace/MDSTest/json/Users.json"; String jsonContext=fileUtil.readFile(filename); JSONArray jsonArray=JSONArray.fromObject(jsonContext); Collection<?> userCollection=JSONArray.toCollection(jsonArray); if(userCollection!=null&&!userCollection.isEmpty()){ Iterator<?> it=userCollection.iterator(); while(it.hasNext()){ JSONObject jsonObj=JSONObject.fromObject(it.next()); User user=(User)JSONObject.toBean(jsonObj, User.class); userList.add(user); } } for(User user:userList){ System.out.println("age:"+user.getAge()); System.out.println("gender:"+user.getGender()); System.out.println("city:"+user.getCity()); System.out.println("score:"+user.getScore()); } }}
上述操作中我们把json中的信息分别保存为4个独立的user类,并将这4个user存储到了List中。这样便完成了json到java对象的转化,也就是json数据的接受。
3.java类转化为json串
- json传递数据
- Ajax传递Json数据
- struts2-json 传递数据
- jquery json 传递数据
- Json数据 传递方式
- JMeter传递JSON数据
- json数据传递
- json数据传递新手入门
- json数据传递
- json传递数据问题
- curl传递json数据
- Ajax传递json数据
- JSON传递数据
- $.ajax方法(传递json数据)
- $.ajax json数据传递方法
- $.ajax json数据传递方法
- JQUERY用JSON传递数据
- iphone利用JSON传递数据
- 使用Nginx实现反向代理
- 003 jdbc入门 statment preparestatment CablleStatement
- USACO08OCT
- list删除时报错ConcurrentModificationException错误
- Python基础篇之If-else
- JSON传递数据
- 20171205_Matlab求方差,均值,均方差,协方差的函数
- Leetcode算法学习日志-720 Longest Word in Dictionary
- 安装openstack时碰到的错误
- aop配置文件记录
- PE头分析
- SAL文档读后感
- Maven学习笔记(二)——创建Maven工程&Maven命令的使用
- UNITY游戏开发源码