package com.test.jsontest;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map; import org.apache.commons.io.FileUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject; //map-json json--file
public class test {
public static void pp() throws JSONException, IOException {
Map<String,String> map=new HashMap();
map.put("1", "qw");
map.put("2", "qwrr");
JSONObject jb=new JSONObject(map);
System.out.println(jb);
FileWriter fileWriter=new FileWriter(new File("test.json"));
jb.write(fileWriter);
fileWriter.flush();
}
//对象-json
public static void pp1() {
stu stu=new stu();
stu.setId(1);
stu.setAdd(new address("huben","shiyi"));
JSONObject jb=new JSONObject(stu);
System.out.println(jb); }
//String-json
public static void pp2() {
String bb="{\"name\":\"lisi\"}";
JSONObject jb=new JSONObject(bb);
System.out.println(jb); }
//file-json
public static void pp3() throws IOException {
InputStream inputStream=new FileInputStream(new File("E:\\per.json")); byte[]bytes=new byte[10];
int len=-1;
StringBuffer sBuffer=new StringBuffer();
while ((len=inputStream.read(bytes))!=-1) {
String string=new String(bytes);
sBuffer.append(string);
}
JSONObject jb=new JSONObject(sBuffer.toString());
System.out.println(jb);
}
//commons-io---json
public static void pp4() throws IOException {
String bb=FileUtils.readFileToString(new File("E:\\per.json")); JSONObject jb=new JSONObject(bb);
System.out.println(jb);
}
//jsonarray
public static void pp5() {
String sj="[{\"name\":\"lisi\"},{\"name1\":\"zs\"}]";
JSONArray jsonArray=new JSONArray(sj);
System.out.println(jsonArray);
} public static void pp6() {
Map<String,String> map=new HashMap();
map.put("1", "qw");
map.put("2", "qwrr");
net.sf.json.JSONArray jArray=new net.sf.json.JSONArray();
jArray=jArray.fromObject(map);
System.out.println(jArray);
}
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
// pp();
// pp1();
// pp2();
// pp3();
// pp4();
// pp5();
pp6();
} } package g1.g1; import java.util.HashMap;
import java.util.Iterator;
import java.util.Map; import net.sf.json.JSONObject;
//map--jsonarray public class test_jsonarray {
public static void pp6() {
Map<String,String> map=new HashMap();
map.put("1", "qw");
map.put("2", "qwrr");
net.sf.json.JSONArray jArray=new net.sf.json.JSONArray();
jArray=jArray.fromObject(map);
System.out.println(jArray);
System.out.println(map);
} //jsonarray--map
public static void pp7() {
String ja="[{\"1\":\"qw\",\"2\":\"qwrr\"},{\"1\":\"qw\",\"2\":\"qwrr\"}]";
net.sf.json.JSONArray jArray=new net.sf.json.JSONArray();
jArray=jArray.fromObject(ja);
Map<String,Object> map=new HashMap();
System.out.println(jArray);
for(int i=0;i<jArray.length()-1;i++) { Object o=jArray.get(i);
JSONObject jObject=(JSONObject)o;
Iterator<String> iterator=jObject.keys();
while (iterator.hasNext()) {
String key = (String) iterator.next();
String value = jObject.getString(key);
map.put(key, value); }
}
System.out.println(map); }
public static void main(String[] args) {
// TODO Auto-generated method stub
// pp6();
pp7();
} }

  

json类型的相互转化的更多相关文章

  1. springmvc接收JSON类型的数据

    1.在使用AJAX传递JSON数据的时候要将contentType的类型设置为"application/json",否则的话会提示415错误 2.传递的data需要时JSON类型的 ...

  2. SpringMVC——对Ajax的处理(包含 JSON 类型)

    一.首先要搞明白的一些事情. 1.从客户端来看,需要搞明白: (1)要发送什么样格式的 JSON 数据才能被服务器端的 SpringMVC 很便捷的处理,怎么才能让我们写更少的代码,如何做好 JSON ...

  3. Struts2+Jquery实现ajax并返回json类型数据

    来源于:http://my.oschina.net/simpleton/blog/139212 摘要 主要实现步骤如下: 1.JSP页面使用脚本代码执行ajax请求 2.Action中查询出需要返回的 ...

  4. JSON.stringify实例应用—将对象转换成JSON类型进行AJAX异步传值

    在上一篇中,对JSON.stringify()方法有了初步的认识,并且做了一些简单的例子.本篇将进一步将JSON.stringify用在复杂些的实例中,例如如下需求: 在进jQuery AJAX异步传 ...

  5. 已知json类型根据类型封装集合

    1编写帮助类根绝url得到json public static string Post(string url) { string strURL = url; //创建一个HTTP请求 HttpWebR ...

  6. 转载:Struts2+Jquery实现ajax并返回json类型数据

    摘要: 主要实现步骤如下: 1.JSP页面使用脚本代码执行ajax请求 2.Action中查询出需要返回的数据,并转换为json类型模式数据 3.配置struts.xml文件 4.页面脚本接受并处理数 ...

  7. json进阶(一)js读取解析JSON类型数据

    js读取解析JSON类型数据 一.什么是JSON? JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式,采用完全独立于语言的文本格式,是理想的数据交换格式,同 ...

  8. HTML基础之JS中的序列化和反序列化-----字符串的json类型与字典之间的相互转换

    前端向后端传递数据的时候不能直接传递对象(如,字典),只能发字符串,Jason就是一种字符串所以前端向后端发送数据的时候,需要将对象转换成字符串 如果前端向后端发送的是json类型,需要通过JSON. ...

  9. mysql 5.7 laravel json类型数据相关操作

    2018年10月16日18:14:21 官方文档中文翻译版 原文:https://dev.mysql.com/doc/refman/5.7/en/json.html 最后有部分实例和一个小总结 11. ...

随机推荐

  1. 2.zabbix自定义模板

    zabbix自定义模板 zbx_base_templates.xml <?xml version="1.0" encoding="UTF-8"?> ...

  2. ZROI week2

    \[ZROI week2\] 除草机 首先考虑最少的拐点肯定是那种螺旋形状的,然后手玩几个数据发现和列数(行数)有关,且每增加1就是上一个状态加2,直接\(O(1)\)公式即可 吐槽:为啥\(n,m\ ...

  3. nodejs操作消息队列RabbitMQ

    一. 什么是消息队列 消息队列(Message Queue,简称MQ),从字面意思上看,本质是个队列,FIFO先入先出,只不过队列中存放的内容是message而已.其主要用途:不同进程Process/ ...

  4. JS-MiniUI:百科

    ylbtech-JS-MiniUI:百科 MINIUI是一款优秀的JS前端web框架,提供丰富.强大控件库,能快速开发企业级Web应用软件.该软件以美观精致的界面和快速的页面响应速度获得用户的好评.是 ...

  5. seleniumIDE command命令

    语法组成要素:command.target.value. command命令 三大类型:(action.Accessor.assertion)   操作  存储  断言 操作类型——Action 浏览 ...

  6. mybatis PageBounds应用分页

    第一步:分页工具类 package com.smartmorse.vo; import java.util.HashMap; import java.util.List; import java.ut ...

  7. 实验吧关于隐写术的writeUp(二)

    0x01 Black Hole 1.下载文件后,发现打不开,放到kali中.用命令file 分析一下文件 root@trial:~/Documents# file blackhole.img blac ...

  8. Notepad++ 连接 FTP 实现编辑 Linux文件

    下载并安装插件 github 下载 :https://github.com/ashkulz/NppFTP/releases/ 安装过程 将下载后解压的文件夹中的 NppFTP.dll 文件,拷贝到 n ...

  9. PAT甲级——A1146 TopologicalOrder【25】

    This is a problem given in the Graduate Entrance Exam in 2018: Which of the following is NOT a topol ...

  10. VS2013+Opencv3.3配置教程

    转载自: https://blog.csdn.net/u014797226/article/details/78283873?locationNum=5&fps=1 参考博文1: 操作环境: ...