导航:首页 > 编程语言 > josnjava

josnjava

发布时间:2022-09-03 22:22:50

A. java如何返回json格式

处理基本的java对象使用JSONObject类,用法大体如下:

public void testMap(){

Map<String,Object> map = new HashMap<String,Object>();

map.put("name", "qiu");

map.put("password", "123");

map.put("address", "china");

User user = new User();

user.setUserName("qiuqiu");

user.setPassword("123456");

user.getTels().add("1234444556677");

user.getTels().add("6893493458585");

map.put("user", user);

JSONObject json = new JSONObject(map);

System.out.println(json.toString());

}

B. java 如何解析JSON

一、JSON(JavaScriptObjectNotation)一种简单的数据格式,比xml更轻巧。Json建构于两种结构:1、“名称/值”对的集合(Acollectionofname/valuepairs)。不同的语言中,它被理解为对象(object),纪录(record),结构(struct),字典(dictionary),哈希表(hashtable),有键列表(keyedlist),或者关联数组(associativearray)。如:{“name”:”jackson”,“age”:100}2、值的有序列表(Anorderedlistofvalues)。在大部分语言中,它被理解为数组(array)如:{“students”:[{“name”:”jackson”,“age”:100},{“name”:”michael”,”age”:51}]}二、java解析JSON步骤A、服务器端将数据转换成json字符串首先、服务器端项目要导入json的jar包和json所依赖的jar包至builtPath路径下(这些可以到JSON-lib官网下载:http://json-lib.sourceforge.net/)然后将数据转为json字符串,核心函数是:(Stringkey,Objectvalue){JSONObjectjsonObject=newJSONObject();jsonObject.put(key,value);returnjsonObject.toString();}B、客户端将json字符串转换为相应的javaBean1、客户端获取json字符串(因为android项目中已经集成了json的jar包所以这里无需导入)publicclassHttpUtil{(StringurlStr){try{//获取HttpURLConnection连接对象URLurl=newURL(urlStr);HttpURLConnectionhttpConn=(HttpURLConnection)url.openConnection();//设置连接属性httpConn.setConnectTimeout(3000);httpConn.setDoInput(true);httpConn.setRequestMethod("GET");//获取相应码intrespCode=httpConn.getResponseCode();if(respCode==200){returnConvertStream2Json(httpConn.getInputStream());}}catch(MalformedURLExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}catch(IOExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}return"";}(InputStreaminputStream){StringjsonStr="";//ByteArrayOutputStream相当于内存输出流ByteArrayOutputStreamout=newByteArrayOutputStream();byte[]buffer=newbyte[1024];intlen=0;//将输入流转移到内存输出流中try{while((len=inputStream.read(buffer,0,buffer.length))!=-1){out.write(buffer,0,len);}//将内存流转换为字符串jsonStr=newString(out.toByteArray());}catch(IOExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}returnjsonStr;}}2、获取(StringjsonStr){Personperson=newPerson();try{//将json字符串转换为json对象JSONObjectjsonObj=newJSONObject(jsonStr);//得到指定jsonkey对象的value对象JSONObjectpersonObj=jsonObj.getJSONObject("person");//获取之对象的所有属性person.setId(personObj.getInt("id"));person.setName(personObj.getString("name"));person.setAddress(personObj.getString("address"));}catch(JSONExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}returnperson;}publicstaticListgetPersons(StringjsonStr){Listlist=newArrayList();JSONObjectjsonObj;try{//将json字符串转换为json对象jsonObj=newJSONObject(jsonStr);//得到指定jsonkey对象的value对象JSONArraypersonList=jsonObj.getJSONArray("persons");//遍历jsonArrayfor(inti=0;i

C. java开发,json是干什么的

json其实就是封装了一种数据格式,它使用了自己定义的标准。主要用来在服务器和客户端的浏览器进行数据交换。因为我们常用的表单形式提交数据,有诸多的不便,json解决了一些问题。

D. Java实现JSON多层遍历

JSONObject jsonObject = new JSONObject(s);
然后用Iterator迭代器遍历取值,建议用反射机制解析到封装好的对象中
JSONObject jsonObject = new JSONObject(jsonString);
Iterator iterator = jsonObject.keys();while(iterator.hasNext()){
key = (String) iterator.next();
value = jsonObject.getString(key);
}

E. 请问Java中json是什么

一 简介:
JSON(JavaScript对象符号)是一种轻量级的数据交换格式。这是很容易为人类所读取和写入。这是易于机器解析和生成。它是基于JavaScript编程语言的一个子集 , 标准ECMA-262第三版- 1999年12月。JSON是一个完全独立于语言的文本格式,但使用C家族的语言,包括C,C + +,C#,Java中的JavaScript,Perl的,Python中,和许多其他程序员所熟悉的约定。这些特性使JSON成为理想的数据交换语言。他和map很类似,都是以
键/值 对存放的。

F. Java解析json数据

一、 JSON (JavaScript Object Notation)一种简单的数据格式,比xml更轻巧。
Json建构于两种结构:
1、“名称/值”对的集合(A collection of name/value pairs)。不同的语言中,它被理解为对象(object),纪录(record),结构(struct),字典(dictionary),哈希表(hash table),有键列表(keyed list),或者关联数组 (associative array)。 如:
{
“name”:”jackson”,
“age”:100
}

2、值的有序列表(An ordered list of values)。在大部分语言中,它被理解为数组(array)如:
{
“students”:
[
{“name”:”jackson”,“age”:100},
{“name”:”michael”,”age”:51}
]
}
二、java解析JSON步骤
A、服务器端将数据转换成json字符串
首先、服务器端项目要导入json的jar包和json所依赖的jar包至builtPath路径下(这些可以到JSON-lib官网下载:http://json-lib.sourceforge.net/)

然后将数据转为json字符串,核心函数是:
public static String createJsonString(String key, Object value)
{
JSONObject jsonObject = new JSONObject();
jsonObject.put(key, value);
return jsonObject.toString();
}
B、客户端将json字符串转换为相应的javaBean
1、客户端获取json字符串(因为android项目中已经集成了json的jar包所以这里无需导入)
public class HttpUtil
{

public static String getJsonContent(String urlStr)
{
try
{// 获取HttpURLConnection连接对象
URL url = new URL(urlStr);
HttpURLConnection httpConn = (HttpURLConnection) url
.openConnection();
// 设置连接属性
httpConn.setConnectTimeout(3000);
httpConn.setDoInput(true);
httpConn.setRequestMethod("GET");
// 获取相应码
int respCode = httpConn.getResponseCode();
if (respCode == 200)
{
return ConvertStream2Json(httpConn.getInputStream());
}
}
catch (MalformedURLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
return "";
}

private static String ConvertStream2Json(InputStream inputStream)
{
String jsonStr = "";
// ByteArrayOutputStream相当于内存输出流
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
// 将输入流转移到内存输出流中
try
{
while ((len = inputStream.read(buffer, 0, buffer.length)) != -1)
{
out.write(buffer, 0, len);
}
// 将内存流转换为字符串
jsonStr = new String(out.toByteArray());
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
return jsonStr;
}
}
2、获取javaBean
public static Person getPerson(String jsonStr)
{
Person person = new Person();
try
{// 将json字符串转换为json对象
JSONObject jsonObj = new JSONObject(jsonStr);
// 得到指定json key对象的value对象
JSONObject personObj = jsonObj.getJSONObject("person");
// 获取之对象的所有属性
person.setId(personObj.getInt("id"));
person.setName(personObj.getString("name"));
person.setAddress(personObj.getString("address"));
}
catch (JSONException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}

return person;
}

public static List<Person> getPersons(String jsonStr)
{
List<Person> list = new ArrayList<Person>();

JSONObject jsonObj;
try
{// 将json字符串转换为json对象
jsonObj = new JSONObject(jsonStr);
// 得到指定json key对象的value对象
JSONArray personList = jsonObj.getJSONArray("persons");
// 遍历jsonArray
for (int i = 0; i < personList.length(); i++)
{
// 获取每一个json对象
JSONObject jsonItem = personList.getJSONObject(i);
// 获取每一个json对象的值
Person person = new Person();
person.setId(jsonItem.getInt("id"));
person.setName(jsonItem.getString("name"));
person.setAddress(jsonItem.getString("address"));
list.add(person);
}
}
catch (JSONException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}

return list;
}

G. java怎么读取json格式的数据

java可以使用JSONObject和JSONArray来操作json对象和json数组,具体用法如下

1:java对象与json串转换:

java对象—json串:

JSONObject JSONStr = JSONObject.fromObject(object);

String str = JSONStr.toString();

json串—java对象:

JSONObject jsonObject = JSONObject.fromObject( jsonString );

Object pojo = JSONObject.toBean(jsonObject,pojoCalss);

2:java数组对象与json串转换:

java数组—json串:

JSONArray arrayStr = JSONArray.fromObject(List<?>);

String str = arrayStr.toString();

json串—java数组:

JSONArray array = JSONArray.fromObject(str);

List<?> list = JSONArray.toList(array, ?.class);

H. java 中如何引用json文件

引用json文件?这个说法够奇怪的。
JSON格式对象的操作可以借助于第三方包JSON-lib(需要其他依赖包)或者org.json(不需要其他依赖包)来操作。
你所说的引用JSON文件,是对json格式的文件进行操作么?如果是那样的话可以利用IO流,读取文件中的字符串,再利用JSON-lib或者org.json进行处理。

阅读全文

与josnjava相关的资料

热点内容
程序员走了 浏览:272
linux设置时间服务器 浏览:282
机械人焊接编程教学视频 浏览:552
体重小本app怎么样 浏览:777
编程语言需求 浏览:935
当兵体重怎么个算法 浏览:170
加密的电脑文件带出去怎么打开 浏览:850
死循环会在编译的时候出错吗 浏览:987
c51单片机特殊寄存器的原理 浏览:578
闪耀永恒特利加密钥 浏览:763
如何夸程序员 浏览:781
天津期货python招聘 浏览:267
单片机机器语言写的程序 浏览:554
韩国直播软件app叫什么名 浏览:920
军营训练不听教官的命令 浏览:263
v开头的音乐播放器是什么APP 浏览:122
单片机是怎么做出来的 浏览:319
博图怎么作为opc服务器 浏览:104
编译做题软件 浏览:298
桥梁检测pdf 浏览:689