首页编程jsonobject,jsonobject怎么获取json中某个值

jsonobject,jsonobject怎么获取json中某个值

编程之家2023-11-04113次浏览

无论是jsonobject还是jsonobject怎么获取json中某个值,它们都是当前热门话题。如果你对它们感到好奇,那么请跟随小编的脚步,一起来揭开它们的秘密吧!

jsonobject,jsonobject怎么获取json中某个值

java中jsonobject和jsonarray的区别

import net.sf.json.JSONArray;

import net.sf.json.JSONObject;

import java.util.*;

public class JavaTest{

public static void main(String[] args){

JSONObject obj=new JSONObject();

jsonobject,jsonobject怎么获取json中某个值

obj.put("derek","23");

obj.put("dad","49");

obj.put("mom","45");

System.out.println("通过构造器的方式创建的JSONObject对象:"+obj);

Map<string,string> map=new LinkedHashMap<>();

map.put("derek","23");

jsonobject,jsonobject怎么获取json中某个值

map.put("dad","49");

map.put("mom","45");

System.out.println("通过fromObject方法将map对象转换为JSONObject对象:"+JSONObject.fromObject(map));

JSONArray arr=new JSONArray();

arr.add(0,"derek");

arr.add(1,"dad");

arr.add(2,"mom");

System.out.println("通过构造器的方式创建的JSONArray:"+arr);

ArrayList list=new ArrayList<>();

list.add("derek");

list.add("dad");

list.add("mom");

System.out.println("通过fromObject方法将Arraylist对象转换为JSONArray对象:"+JSONArray.fromObject(list));

System.out.println("将HashMap对象通过fromObject方法转换为JSONArray对象"+JSONArray.fromObject(map));

String str="{\"derek\":23,\"dad\":49,\"mom\":45}";

System.out.println("解析之后的JSON对象:"+JSONObject.fromObject(str));

//遍历输出

Iterator it=obj.keys();

while(it.hasNext()){

String key=it.next();

System.out.println(key+":"+obj.get(key));

}

}

}

运行结果如下:

通过构造器的方式创建的JSONObject对象:{"derek":"23","dad":"49","mom":"45"}

通过fromObject方法将map对象转换为JSONObject对象:{"derek":"23","dad":"49","mom":"45"}

通过构造器的方式创建的JSONArray:["derek","dad","mom"]

通过fromObject方法将Arraylist对象转换为JSONArray对象:["derek","dad","mom"]

将HashMap对象通过fromObject方法转换为JSONArray对象[{"derek":"23","dad":"49","mom":"45"}]

解析之后的JSON对象:{"derek":23,"dad":49,"mom":45}

derek:23

dad:49

mom:45

Java List是个集合接口,只要是集合类接口都会有个“迭代子”( Iterator),利用这个迭代子,就可以对list内存的一组对象进行操作。所有要想操作这个list内存的东西,就首先要得到此迭代子的实例:Iterator it=l.iterator();

用add()方法即可添加新的成员对象,他可以添加的仅仅只能为对象,不能添加基本数据类型,容器还对应get(),remove()方法来获取和删除数据成员

实例1.

import java.util.*;

public class ArrayListTest{

public static void main(String dd[]){

//new了一个存储list

List l=new ArrayList();

//因为Collection framework只能存储对象所以new封装类

l.add(new Integer(1));

l.add(new Integer(2));

l.add(new Integer(3));

l.add(new Integer(4));

Iterator it=l.iterator();

//hasNext是取值取的是当前值.他的运算过程是判断下个是否有值如果有继续.

while(it.hasNext()){

//设it.next封装类,调用Integer的intValue方法返回值为int赋给i;

int i=((Integer)it.next()).intValue();

System.out.println("Element in list is:"+i);}}}

ArrayList list= new ArrayList()和List<String> list= new ArrayList<String>()的区别??

1、存储内容的区别

ArrayList可以存储任何类型的项

List<类型>只可以存储指定类型的项

2、使用是否方便

List<>比ArrayList使用方便

因为在使用ArrayList内部的值时,必须强制转换才行

相信使用过ArrayList的都深有体会,因为存放在ArrayList里的值都转换成了Object类型

3、出现的时间

ArrayList比List<>出现的时间早

List<>是C#2.0时出现的,且是其最强大的功能之一

4、所在命名空间的区别

ArrayList的命名空间是System.Collections

List<>的命名空间是System.Collections.Generic

其中Generic是泛型的意思,使用List<>也就是在使用泛型技术

5、编程中的使用频率

大多数情况下 ArrayList可以废弃不用,毕竟其是C#2.0之前的技术了

这一点从在Visual Studio中新建一个类时便可以看出

新建一个类时,其默认使用的命名空间有

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

显然没有System.Collections,因为ArrayList的使用的确不方便

fastjson json和jsonobject的区别

要使用Fastjson,首先需要下载相对应的jar文件,在官网即可下载。

附上初学的第一个例子,多多指教:

复制代码

{

"statuses":[

{

"id": 912345678901,

"text":"How do I stream JSON in Java?",

"geo": null,

"user":{

"name":"json_newb",

"followers_count": 41

}

},

{

"id": 777777777888,

"text":"dfngsdnglnsldfnsl",

"geo": null,

"user":{

"name":"dsfgpd",

"followers_count": 24

}

}

]

}

复制代码

AllBean的Bean类:

复制代码

package com.lee.JsonToBean;

public class AllBean{

private long id;

private String text;

private String geo;

private UserBean userBean;

public long getId(){

return id;

}

public void setId(long id){

this.id= id;

}

public String getText(){

return text;

}

public void setText(String text){

this.text= text;

}

public String getGeo(){

return geo;

}

public void setGeo(String geo){

this.geo= geo;

}

public UserBean getUserBean(){

return userBean;

}

public void setUserBean(UserBean userBean){

this.userBean= userBean;

}

}

复制代码

UserBean的Bean类:

复制代码

package com.lee.JsonToBean;

public class UserBean{

private String name;

private int followers_count;

public String getName(){

return name;

}

public void setName(String name){

this.name= name;

}

public int getFollowers_count(){

return followers_count;

}

public void setFollowers_count(int followers_count){

this.followers_count= followers_count;

}

}

复制代码

解析类JsonBean:

复制代码

package com.lee.JsonToBean;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.IOException;

import java.util.List;

import javax.swing.text.BadLocationException;

import javax.swing.text.DefaultStyledDocument;

import javax.swing.text.rtf.RTFEditorKit;

import com.alibaba.fastjson.JSON;

import com.alibaba.fastjson.JSONArray;

import com.alibaba.fastjson.JSONObject;

/**

*{

"statuses":[

{

"id": 912345678901,

"text":"How do I stream JSON in Java?",

"geo": null,

"user":{

"name":"json_newb",

"followers_count": 41

}

},

{

"id": 777777777888,

"text":"dfngsdnglnsldfnsl",

"geo": null,

"user":{

"name":"dsfgpd",

"followers_count": 24

}

}

]

}

**/

public class JsonBean{

RTFEditorKit rtf;

DefaultStyledDocument dsd;

String text;

public static void main(String[] args){

JsonBean bean= new JsonBean();

//把字符串转为Json对象,这是因为我的json数据首先是json对象

JSONObject jobj= JSON.parseObject(bean.readRtf(new File("json.rtf")));

//然后是jsonArray,可以根据我的json数据知道

JSONArray arr= jobj.getJSONArray("statuses");

//根据Bean类的到每一个json数组的项

List<AllBean> listBeans= JSON.parseArray(arr.toString(), AllBean.class);

//遍历

for(AllBean bean_: listBeans){

//我这个demo的json数据获得第一层的数据

System.out.println(bean_.getText());

System.out.println(bean_.getId());

//我这个demo的json数据获得第二层的数据

System.out.println(bean_.getUserBean().getFollowers_count());

}

}

//因为我把json数据放进rtf文件,这是读取rtf文件的json数据,转化为字符串

public String readRtf(File in){

rtf=new RTFEditorKit();

dsd=new DefaultStyledDocument();

try{

rtf.read(new FileInputStream(in), dsd, 0);

text= new String(dsd.getText(0, dsd.getLength()));

} catch(FileNotFoundException e){

// TODO Auto-generated catch block

e.printStackTrace();

} catch(IOException e){

// TODO Auto-generated catch block

e.printStackTrace();

} catch(BadLocationException e){

// TODO Auto-generated catch block

e.printStackTrace();

}

return text;

}

}

jsonobject怎么获取json中某个值

这样:

public static void jsonToBean(String data){

try{

JSONArray array= new JSONArray(data);//将json字符串转成json数组

for(int i= 0; i< array.length(); i++){//循环json数组

JSONObject ob=(JSONObject) array.get(i);//得到json对象

String name= ob.getString("name");//name这里是列名称,获取json对象中列名为name的值

System.out.print(name);//输出name

} catch(JSONException e){

}

}

扩展资料:

注意事项

java处理 json格式字符串:转成 JSONArray或 JSONObject类型

1、如果是JSONArray,格式:最外层是中括号,表示数组

格式: [{key:value},{key:value}... ]

["str1","str2","str3",...]

语法:JSONArray array= JSONArray.parseArray(strs)

注:strs必须是json格式的字符串,以"[ ]"中括号开头结尾.否则会报错.

2、如果是JSONObject,格式:最外层是大括号,表示对象

格式:{key:value}

语法::JSONObject result= JSONArray.parseObject(strs);

文章分享结束,jsonobject和jsonobject怎么获取json中某个值的答案你都知道了吗?欢迎再次光临本站哦!

headers_sent(...header information - headers already sent的问题)软件系统定制开发(软件定制开发、模板软件、SAAS系统三者的区别及概念)