目录
转换json的时候报错:
net.sf.json.JSONException: java.lang.NoSuchMethodException: Property 'name' has no getter method
at net.sf.json.JSONObject.defaultBeanProcessing(JSONObject.java:818)
at net.sf.json.JSONObject._fromBean(JSONObject.java:699)
at net.sf.json.JSONObject.fromObject(JSONObject.java:172)
at net.sf.json.AbstractJSON._processValue(AbstractJSON.java:274)
at net.sf.json.JSONArray._processValue(JSONArray.java:2513)
at net.sf.json.JSONArray.processValue(JSONArray.java:2538)
at net.sf.json.JSONArray.addValue(JSONArray.java:2525)
at net.sf.json.JSONArray._fromCollection(JSONArray.java:1056)
at net.sf.json.JSONArray.fromObject(JSONArray.java:123)
at net.sf.json.JSONArray.fromObject(JSONArray.java:105)
at com.css.eshop.test.testJSon.test(testJSon.java:52)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.ecl
测试的时候想着方便一点,写在了一起,先看看源码:(Student类是同一个test里)
package com.css.eshop.test;
import static org.junit.Assert.*;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import org.junit.Test;
import net.sf.json.JSONArray;
import net.sf.json.JsonConfig;
class Student {
private String name;
private String password;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
public class testJSon {
@Test
public void test() {
List<Student> attrList = new ArrayList<Student>();
Student sti=new Student();
sti.setName("小可爱");sti.setPassword("12345");
Student sti2=new Student();
sti2.setName("猪头");sti2.setPassword("12345");
attrList.add(sti);attrList.add(sti2);
String attrJson = JSONArray.fromObject(attrList).toString();
System.out.println(attrJson);
}
}
测试的时候,报错:
解决方法1:
(1)分开写。
package com.css.eshop.test;
import static org.junit.Assert.*;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import org.junit.Test;
import net.sf.json.JSONArray;
public class testJSon {
@Test
public void test() {
List<Student> attrList = new ArrayList<Student>();
Student sti=new Student();
sti.setName("小可爱");sti.setPassword("12345");
Student sti2=new Student();
sti2.setName("猪头");sti2.setPassword("12345");
attrList.add(sti);attrList.add(sti2);
/*System.out.println(attrList);*/
String attrJson = JSONArray.fromObject(attrList).toString();
System.out.println(attrJson);
}
}
package com.css.eshop.test;
public class Student {
private String name;
private String password;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
测试结果:
解决方法2:一般用安卓自带的,或者谷歌的Gson
(1)导入gson的jar包。
(2)编写代码
package com.css.eshop.test;
import static org.junit.Assert.*;
import org.junit.Test;
import java.util.ArrayList;
import java.util.List;
import com.google.gson.Gson;
class Student1 {
public String name;
public String password;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "Student [name=" + name + ", password=" + password + "]";
}
}
public class test2 {
public static void main(String[] args) {
test();
}
public static void test() {
List<Student1> attrList = new ArrayList<>();
Student1 sti=new Student1();
sti.setName("小可爱");sti.setPassword("12345");
Student1 sti2=new Student1();
sti2.setName("猪头");sti2.setPassword("12345");
attrList.add(sti);
attrList.add(sti2);
Gson gson = new Gson();
String json = gson.toJson(attrList);
System.out.println(json);
}
}
(3)测试结果: