第一个程序
package bll.sap;
import java.net.MalformedURLException;
import utility.PropertyUtility;
public class GetActiveData
{
public static void main(String[] args) {
String sapURL = "";
String transactionSapURL="";
try {
sapURL = PropertyUtility.getSapURL();
transactionSapURL = PropertyUtility.getTransactionSapURL();
//Get Summary Data
//SapDataSync sapDataSync = new SapDataSync();
SapDataSync sapDataSync = new SapDataSync();
//sapDataSync.readTransactionJsonFromUrl(sapURL+"E13F42EC5E38");
sapDataSync.readTransactionJsonFromUrl("http://localhost/uatpw/ActiveTransaction?isx=E13F5AFA45CE");
}
catch(MalformedURLException me)
{
me.printStackTrace();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
第二个节目
package bll.sap;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import net.sf.json.JSONArray;
import net.sf.json.JSONException;
import net.sf.json.JSONObject;
import utility.Utility;
import com.google.code.morphia.Datastore;
import com.sun.jndi.toolkit.url.Uri;
import dal.GetMorphiaDB;
public class SapDataSync
{
private void saveSapTransaction(List<TransactionUnit> sapTransaction){
GetMorphiaDB morphia;
try {
morphia = GetMorphiaDB.getInstance();
Datastore ds = morphia.getDs();
ds.save(sapTransaction);
}catch (Exception e) {
e.printStackTrace();
}
}
private void createSapTransaction(String transactionJson)
{
JSONObject jsonObj = JSONObject.fromObject(transactionJson);
JSONArray transactionUnits = jsonObj.getJSONArray("TRANSACTION");
//System.out.println("transactionUnits="+transactionUnits);
List<ActiveUnit> transactionList = new ArrayList<ActiveUnit>();
for(int i = 0; i < transactionUnits.size() ; i++)
{
JSONObject jsn = transactionUnits.getJSONObject(i);
ActiveUnit transactionUnit = new ActiveUnit
(
jsn.getString("listEditions"),
jsn.getString("listPackage"),
jsn.getString("referenceID")
);
//System.out.println("transactionUnit ="+transactionUnit);
transactionList.add(transactionUnit);
//System.out.println("transactionList ="+transactionList);
}
//System.out.println(transactionList.size());
if (transactionList.size() > 0) {
//saveSapTransaction(transactionList);
}
}
public void readTransactionJsonFromUrl(String url) throws IOException, JSONException
{
InputStream is = new URL(url).openStream();
try
{
BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
StringBuilder sb = new StringBuilder();
int cp;
while ((cp = rd.read()) != -1)
{
sb.append((char) cp);
}
createSapTransaction(sb.toString());
}
finally
{
is.close();
}
}
}
第三期节目
package bll.sap;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
import org.bson.types.ObjectId;
import com.google.code.morphia.annotations.Entity;
import com.google.code.morphia.annotations.Id;
@Entity("SapTransaction")
public class ActiveUnit implements Serializable {
private static final long serialVersionUID = 1L;
@Id
private String listEditions;
private String listPackage;
private String referenceID;
private List<ActiveUnit> listEdition;
public ActiveUnit() {
}
public ActiveUnit(String listEdtions, String listPackage,String referenceID) {
this.listEditions = listEdtions;
this.listPackage = listPackage;
this.referenceID = referenceID;
}
public String getreferenceID() {
return referenceID;
}
public void setreferenceID(String referenceID) {
this.referenceID = referenceID;
}
public String getListEditions() {
return listEditions;
}
public void setListEditions(String listEditions) {
this.listEditions = listEditions;
}
public String getListPackage() {
return listPackage;
}
public void setListPackage(String listPackage) {
this.listPackage = listPackage;
}
@Override
public String toString()
{
String unit = "{ " +"listEditions: " + this.listEditions+
",listPackage: "+ this.listPackage+
",referenceID: " + this.referenceID+
"}";
return unit;
}
}
运行第一个程序后,我从该 url 获取 JSON 格式的数据,如下所示。
{
"TRANSACTION":[
{
"listEditions": [
{
"adRoute":"B",
"listInsertion":[
{
"color":0,"colorType":"All Colour","fromDate":"26/06/2013","height":25,"page":2,"pagePosition":2,"pagePositionType":"Regular Page","size":823,"sizeDimention":"32.9x25","toDate":"26/06/2013","width":32.9,"pubDate":""
}
]
},
{
"adRoute":"A",
"listInsertion":[
{ "color":0,"colorType":"All Colour","fromDate":"26/06/2013","height":25,"page":2,"pagePosition":2,"pagePositionType":"Regular Page","size":823,"sizeDimention":"32.9x25","toDate":"26/06/2013","width":32.9,"pubDate":""
}
]
},
{
"adRoute":"A",
"listInsertion":[
{ "color":0,"colorType":"All Colour","fromDate":"26/06/2013","height":25,"page":2,"pagePosition":2,"pagePositionType":"Regular Page","size":823,"sizeDimention":"32.9x25","toDate":"26/06/2013","width":32.9,"pubDate":""
}
]
},
{
"adRoute":"A",
"listInsertion":[
{ "color":0,"colorType":"All Colour","fromDate":"26/06/2013","height":25,"page":2,"pagePosition":2,"pagePositionType":"Regular Page","size":823,"sizeDimention":"32.9x25","toDate":"26/06/2013","width":32.9,"pubDate":""
}
]
},
{
"adRoute":"A",
"listInsertion":[
{ "color":0,"colorType":"All Colour","fromDate":"26/06/2013","height":25,"page":2,"pagePosition":2,"pagePositionType":"Regular Page","size":823,"sizeDimention":"32.9x25","toDate":"26/06/2013","width":32.9,"pubDate":""
}
]
},
{
"adRoute":"A",
"listInsertion":[
{ "color":0,"colorType":"All Colour","fromDate":"26/06/2013","height":25,"page":2,"pagePosition":2,"pagePositionType":"Regular Page","size":823,"sizeDimention":"32.9x25","toDate":"26/06/2013","width":32.9,"pubDate":""
}
]
}
],
listPackage:
[
{
listEditions: [
{
"adRoute":"B",
"listInsertion":[
{
"color":0,"colorType":"All Colour","fromDate":"26/06/2013","height":25,"page":2,"pagePosition":2,"pagePositionType":"Regular Page","size":823,"sizeDimention":"32.9x25","toDate":"26/06/2013","width":32.9,"pubDate":""
}
]
},
{
"adRoute":"A",
"listInsertion":[
{ "color":0,"colorType":"All Colour","fromDate":"26/06/2013","height":25,"page":2,"pagePosition":2,"pagePositionType":"Regular Page","size":823,"sizeDimention":"32.9x25","toDate":"26/06/2013","width":32.9,"pubDate":""
}
]
},
{
"adRoute":"A",
"listInsertion":[
{ "color":0,"colorType":"All Colour","fromDate":"26/06/2013","height":25,"page":2,"pagePosition":2,"pagePositionType":"Regular Page","size":823,"sizeDimention":"32.9x25","toDate":"26/06/2013","width":32.9,"pubDate":""
}
]
},
{
"adRoute":"A",
"listInsertion":[
{ "color":0,"colorType":"All Colour","fromDate":"26/06/2013","height":25,"page":2,"pagePosition":2,"pagePositionType":"Regular Page","size":823,"sizeDimention":"32.9x25","toDate":"26/06/2013","width":32.9,"pubDate":""
}
]
},
{
"adRoute":"A",
"listInsertion":[
{ "color":0,"colorType":"All Colour","fromDate":"26/06/2013","height":25,"page":2,"pagePosition":2,"pagePositionType":"Regular Page","size":823,"sizeDimention":"32.9x25","toDate":"26/06/2013","width":32.9,"pubDate":""
}
]
},
{
"adRoute":"A",
"listInsertion":[
{ "color":0,"colorType":"All Colour","fromDate":"26/06/2013","height":25,"page":2,"pagePosition":2,"pagePositionType":"Regular Page","size":823,"sizeDimention":"32.9x25","toDate":"26/06/2013","width":32.9,"pubDate":""
}
]
}
]
}],
"referenceID": "E13F42EC5E38"
}
]
}
现在的问题是如何从内部包含内部数组的 JSON 数据中读取所有键和值。我可以从那个 TRANSACTION Array 中读取数据。但进一步,如果我想从该 listEditions 数组和 listEditions 数组内的数组 listInsertion 读取数据,那么它会给我带来问题。但是我尝试了一些方法,但在静脉中。请帮忙。当前代码仅用于从 TRANSACTION Array 中读取数据。我不知道如何编写代码以从其他数组中读取数据。我试图阅读它,但它说它不是一个数组,它是一个字符串。