三、json解析方式
String strSubs = chapter1Object.getString("xxx");
JSONObject subsObject = new JSONObject(strSubs);
Iterator iter = subsObject.keys();
while (iter.hasNext()) {
String str = (String) iter.next();
System.out.println("子课程id 键名:" + str);
String strSub = subsObject.getString(str);
JSONObject subObject = new JSONObject(strSub);
Subsection sub = new Subsection(subObject.getInt(" xxx ")
subObject.getString(" xxx "),
subObject.getString(" xxx "),
subObject.getString(" xxx "));
chapter.getSubsList().add(sub);
}
二、 json解析时遇到因json数据量太大,通过2中方式获取到的json数据都不完整
用System.out.println 和 Log 来打印过, 发现是有长度限制的, 如果要打印的数据太大, 是无法完全打印出来的. 但是json的数据是完整的. 这以前也纠结了我好久. 所以你直接的解析json就可以了, 如果你还是想看json是否完整的话, 你可以这样, 将json通过流的方式保存到sd卡里去,再导出来就可以看到json是否完整了.
一、j2ee使用json传输数据
转载地址http://blog.sina.com.cn/s/blog_5920510a01011vu8.html
JSON 字符串 与 java 对象的转换 (2012-03-23 12:30:09)转载�
标签: json java 分类: JSON-JAVA
jsonLib 经典文章:http://json-lib.sourceforge.net/xref-test/net/sf/json/TestJSONObject.html
// 引入相应的包
//json-lib-2.2-jdk15.jar
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
1. //把java 对象列表转换为json对象数组,并转为字符串
JSONArray array = JSONArray.fromObject(userlist);
String jsonstr = array.toString();
2.//把java对象转换成json对象,并转化为字符串
JSONObject object = JSONObject.fromObject(invite);
Log4jInit.ysulogger.debug(object.toString());
3.//把JSON字符串转换为JAVA 对象数组
String personstr = getRequest().getParameter("persons");
JSONArray json = JSONArray.fromObject(personstr);
List<InvoidPerson> persons = (List<InvoidPerson>)JSONArray.toCollection(json, nvoidPerson.class);
4.//把JSON字符串转换为JAVA 对象
str = "{\"lendperson\":\"李四\",\"lendcompany\":\"有限公司\",\"checkperson\":\"李四\",
\"lenddate\":\"2010-07-19T00:00:00\",\"lendcounts\":4,\"
passports\":[{\"passportid\":\"d\",\"name\":\"李豫川\",\"passporttype\":\"K\"},
{\"passportid\":\"K9051\",\"name\":\"李平\",\"passporttype\":\"K\"},
{\"passportid\":\"K90517\",\"name\":\"袁寒梅\",\"passporttype\":\"K\"},
{\"passportid\":\"K905199\",\"name\":\"贺明\",\"passporttype\":\"K\"}]}";
JSONObject jsonobject = JSONObject.fromObject(str);
PassportLendsEntity passportlends = null;
try {
//获取一个json数组
JSONArray array = jsonobject.getJSONArray("passports");
//将json数组 转换成 List<PassPortForLendsEntity>泛型
List<PassPortForLendsEntity> list = new ArrayList<PassPortForLendsEntity>();
for (int i = 0; i < array.size(); i++) {
JSONObject object = (JSONObject)array.get(i);
PassPortForLendsEntity passport = (PassPortForLendsEntity)JSONObject.toBean(object,
PassPortForLendsEntity.class);
if(passport != null){
list.add(passport);
}
}
//转换PassportLendsEntity 实体类
passportlends = (PassportLendsEntity)JSONObject.toBean(jsonobject, PassportLendsEntity.class);
android端json使用
public void getJson(View view){
System.out.println("--------------------------获取Json数据测试!");
new Thread(){
@Override
public void run() {
System.out.println("---------------------启动线程访问网络!");
httpServer=new HttpServer(MainActivity.this);
String result = "";
PrintWriter out;
try {
out = new java.io.PrintWriter(// 用来输出字符流
httpServer.URLConnection.getOutputStream());
out.println("op=getData");
out.flush();
out.close();
java.io.BufferedReader bReader = new java.io.BufferedReader(
new java.io.InputStreamReader(
httpServer.URLConnection.getInputStream()));
result = bReader.readLine().toString().trim();
System.out.println("--------------------result:" + result);
Gson gson = new Gson();
JSONObject object = new JSONObject(result);
System.out.println("-----------------obj name:"+object.get("name"));
Person person=gson.fromJson(result, Person.class); //将json字符串转换成java对象
// ObjectMapper objectMapper = new ObjectMapper();
// JsonGenerator jsonGenerator = objectMapper.getJsonFactory().createJsonGenerator(System.out, JsonEncoding.UTF8);;
//
// Person person=objectMapper.readValue(object, Person.class);
System.out.println("---------------------person.Name:"+person.getName());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}.start();
}
当抛出异常E/AndroidRuntime(26691):
com.google.gson.JsonSyntaxException: java.lang.NumberFormatException: Invalid double: "admin"时。
意思是在将json字符串转换成java对象的对象属性数据类型和字符串给的值类型不一致。
如果你不认识你首先应该去查JDK的帮助文档,上面是这样写的
public class NumberFormatExceptionextends IllegalArgumentException
当应用程序试图将字符串转换成一种数值类型,但该字符串不能转换为适当格式时,抛出该异常。