Json   发布时间:2022-04-22  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了JSONArray和JSONObject的使用大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

Json 应用

根据对像生成字符串
private String getWholeSALEPriceJson(List<WholeSALEProductPriceDTO> priceDtolist) {
List<WholeSALEPriceDTO> priceList = new ArrayList<WholeSALEPriceDTO>();
if (priceDtolist != null) {
for (WholeSALEProductPriceDTO wholeSALEProductPriceDTO : priceDtolist) {
priceList.add(new WholeSALEPriceDTO(getWholeSALEStandardPricEIDList(wholeSALEProductPriceDTO),
wholeSALEProductPriceDTO.getStandard()));
}
}
return JSONArray.fromObject(priceList).toString();
}

页面显示与操作:

function checkStandard(){
var YLJ = YAHOO.lang.JSON;
var priceList = YLJ.parse(document.getElementById("wholeSALEPrice").value);
//开始校验重复数据
var map={};
for (var i = 0; i < priceList.length; i++) {
if(priceList[i].value.length > 32) {
alert("您好,产品介格中产品规则字段长度超过32!");
return false;
}
if(map[priceList[i].value] == null) {
map[priceList[i].value] = 1;
} else {
alert("您好,产品价格中产品规则字段有重复值!");
return false;
}
}
return true;
}

再转换回去成为对象.

JSONArray array = JSONArray.fromObject(json);
return (List<WholeSALEPriceDTO>) JSONArray.toCollection(array,WholeSALEPriceDTO.class);

这样简单的应用就完成了

()

//使用JSONObject JSONArray 类
JSONArray array = new JSONArray();
JSONObject jobject = new JSONObject();
String titles = "<a href=/"http://www.163.com/">我是/"title/"</a>";
for (int n = 0; n < 5; n++){
JSONObject jo = new JSONObject();
jo.put("id",n);
jo.put("title",titles + n);
jo.put("cruser","cruser"+n);
jo.put("crdate","crdate"+n);
jo.put("udtuser","udtuser"+n);
jo.put("udtdate","udtdate"+n);
jo.put("hits","hits"+n);
jo.put("hot",truE);
jo.put("ordernum",n);
jo.put("status","status"+n);
array.put(jo);
}
jobject.put("@R_303_10586@lCount",30);
jobject.put("root",array);
System.out.println(jobject);


记得
import java.util.*;
import org.json.*;

还有try catch




(补充)

package com.longtop.ajax.json;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;


public class TestServlet {
public static void main(String[] args){
TestServlet ts=new TestServlet();
System.out.println(ts.dellJson());

//JSONObjectjo=JSONObject.fromObject(ts.dellJson());


try{
JSONObjectjo=newJSONObject(ts.dellJson());
JSONArray asonArray = jo.getJSONArray("addressbood");
Object o = asonArray.get(1);
JSONObjectjo1 = (JSONObject)o;
System.out.println(jo1.toString());

//JSONArray array=jo1.getJSONArray("address");
System.out.println(jo1.get("address"));
JSONArray asonArray1 = jo1.getJSONArray("address");
JSONObjectjo11 = (JSONObject)asonArray1.get(0);
System.out.println(jo11.getString("city"));
}catch(JSONException a){
a.printStackTrace();
}

}
public String dellJson(){
String str="";
Map map=new HashMap();
Address MaryLebow = new Address("5 Main Street","San Diego,CA","91912","619-332-3452","664-223-4667");
map.put("Mary Lebow",MaryLebow);

Address amySmith = new Address("25 H Street","Los Angeles,"95212","660-332-3452","541-223-4667");
map.put("Sally May",amySmith);

Address johnKim = new Address("2343 Sugarland Drive","Houston,TX","55212","554-332-3412","461-223-4667");
map.put("John Kim",johnKim);

Address richardThorn = new Address("14 68th Street","New York,NY","12452","212-132-6182","161-923-4001");
map.put("Richard Thorn",richardThorn);
Iterator it=map.entrySet().iterator();

JSONArray ja=new JSONArray();
while(it.hasNext()){
Map.Entry e=(Map.Entry)it.next();
String name=(String)e.getKey();
Address add=(Address)e.getValue();
String city=add.getCity();
String street=add.getStreet();
String zip=add.getZip();
String phone1=add.getPhoneNumber1();
String phone2=add.getPhoneNumber2();
try{
JSONObjectjbAddress=newJSONObject();
jbAddress.append("street",street);
jbAddress.append("city",city);
jbAddress.append("zip",zip);
JSONArray jaPhone=new JSONArray();
jaPhone.put( phone1);
jaPhone.put( phone2);
JSONObjectjbAddressBook=newJSONObject();
jbAddressBook.append("name",Name);
jbAddressBook.append("address",jbAddress);
jbAddressBook.append("phonenumbers",jaPhonE);
ja.put(jbAddressBook);

}catch(JSONException a){
a.printStackTrace();
}

}
try{
str=newJSONObject().put("addressbood",ja).toString(); }catch(JSONException a){ a.printStackTrace(); } return str; } }

大佬总结

以上是大佬教程为你收集整理的JSONArray和JSONObject的使用全部内容,希望文章能够帮你解决JSONArray和JSONObject的使用所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。