package com;
import javautilList;
import comKensouPerson;
import comalibabafastjsonJSON;
public class Yuma
{
public static void main(String[] args)
{
String result =
"{\"data1\":[{\"name\":\"aa\",\"age\":\"12\"},{\"name\":\"bb\",\"age\":\"13\"}],\"data2\":{\"nowpage\":1,\"pagesize\":2}}";
Kensou ps = JSONparseObject(result, Kensouclass);
Systemoutprintln(ps);
List<Person> data1 = psgetData1();
for(int i = 0; i < data1size(); i++)
{
Person p = data1get(i);
Systemoutprintln(p);
}
Person data2 = psgetData2();
Systemoutprintln(data2);
}
}
/
模型对象不需要构造函数, 至少只需要setter方法<br>
Person类可以是内嵌类,也可以是外部类 <br>
Kensou类只能是外部类<br>
集合类可以是数组或者List类型<br>
<p>
使用的是<font color="red">fastjson-1136jar</font>
/
class Kensou
{
private List<Person> data1;
private Person data2;
public List<Person> getData1()
{
return data1;
}
public void setData1(List<Person> data1)
{
thisdata1 = data1;
}
public Person getData2()
{
return data2;
}
public void setData2(Person data2)
{
thisdata2 = data2;
}
@Override
public String toString()
{
return Stringformat("Kensou [data1=%s, data2=%s]", data1, data2);
}
class Person
{
private String name;
private int age;
private int nowpage;
private int pagesize;
public String getName()
{
return name;
}
public void setName(String name)
{
thisname = name;
}
public int getAge()
{
return age;
}
public void setAge(int age)
{
thisage = age;
}
public int getNowpage()
{
return nowpage;
}
public void setNowpage(int nowpage)
{
thisnowpage = nowpage;
}
public int getPagesize()
{
return pagesize;
}
public void setPagesize(int pagesize)
{
thispagesize = pagesize;
}
@Override
public String toString()
{
return Stringformat("Person [name=%s, age=%s, nowpage=%s, pagesize=%s]", name, age, nowpage, pagesize);
}
}
}
public static void jsonToBean(String data) {
try {
JSONArray array = new JSONArray(data);//将json字符串转成json数组
for (int i = 0; i < arraylength(); i++) {//循环json数组
JSONObject ob = (JSONObject) arrayget(i);//得到json对象
String name= obgetString("name");//name这里是列名称,获取json对象中列名为name的值
Systemoutprint(name);//输出name
} catch (JSONException e) {
}
}
for(int i=0;i<listlength;i++){
var nop = list[i];
if(nopid==3){
alert(你想要的)
}
}
如果不是Android开发环境的话,首先需要引入处理JSON数据的包:json-lib-223-jdk15jar
Java样例程序如下:
import netsfjsonJSONArray;import netsfjsonJSONObject;
public class DoJSON {
public static void main(String[] args) {
JSONArray employees = new JSONArray(); //JSON数组
JSONObject employee = new JSONObject(); //JSON对象
employeeput("firstName", "Bill"); //按“键-值”对形式存储数据到JSON对象中
employeeput("lastName", "Gates");
employeesadd(employee); //将JSON对象加入到JSON数组中
employeeput("firstName", "George");
employeeput("lastName", "Bush");
employeesadd(employee);
employeeput("firstName", "Thomas");
employeeput("lastName", "Carter");
employeesadd(employee);
Systemoutprintln(employeestoString());
for(int i=0; i<employeessize(); i++) {
JSONObject emp = employeesgetJSONObject(i);
Systemoutprintln(emptoString());
Systemoutprintln("FirstName :\t" + empget("firstName"));
Systemoutprintln("LastName : \t" + empget("lastName"));
}
}
}
运行效果:
[{"firstName":"Bill","lastName":"Gates"},{"firstName":"George","lastName":"Bush"},{"firstName":"Thomas","lastName":"Carter"}]
{"firstName":"Bill","lastName":"Gates"}
FirstName : Bill
LastName : Gates
{"firstName":"George","lastName":"Bush"}
FirstName : George
LastName : Bush
{"firstName":"Thomas","lastName":"Carter"}
FirstName : Thomas
LastName : Carter
以上就是关于json对象的值为数组,怎么获取数组某项的值(注意不是json数组)全部的内容,包括:json对象的值为数组,怎么获取数组某项的值(注意不是json数组)、jsonobject怎么获得json数组里面的值、怎么获取json数组里面对象的每一个值等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)