我有这样的 JSON 响应。我尝试了很多但无法解析值。
{
"ResponseCode": "000",
"ResponseDescription": "Successful",
"SystemServiceID": [
"4"
],
"SystemServiceName": [
"Test Name"
],
"ProductID": [
"10"
],
"ProductName": [
"Testing"
],
"ProductDescription": [
"Test product"
],
"MinimumValue": [
10000
],
"MaximumValue": [
500000
],
"ImageURL": [
null
],
"Country": [
"Test"
],
"CompanyID": [
"1"
],
"CompanyName": [
"Test"
],
"FieldLevel": [
"2"
],
"FieldInfo": [
"{\"Field1\":{\"Field Name\":\"Phone Number\",\"Field Type\":\"Number\",\"Validation\":{\"Min\":\"4\",\"Max\":\"8\"}},\"Field2\":{\"Field Name\":\"Email\",\"Field Type\":\"String\",\"Validation\":{\"Regular Expression\":\"aaaaa\",\"Min Length\":\"10\",\"Max Length\":\"20\"}}}"
]
}
我有最后一个字段FieldInfo
,它具有来自服务器的动态字段 1、字段 2,这意味着有时 FieldInfo 有 2 个字段或有时 3 个字段或有时它有 4 个字段等等。
如果它有 2 个字段,那么它应该是 Field1,Field2
如果它有 3 个字段,那么它应该是 Field1,Field2,Field3 等等。
代码
private ArrayList<BillPayProduct> parseBillPayResult(String data)
{
try
{
final JSONObject jsonObject=new JSONObject(data);
JSONArray jsonArray;
if(jsonObject.getString("ResponseCode").equals("000"))/*&&jsonObject.getString("ResponseDescription").equals("Successful"))*/
{
sysSerID=new ArrayList<String>();
sysSerName=new ArrayList<String>();
productID=new ArrayList<String>();
productName=new ArrayList<String>();
productDesc=new ArrayList<String>();
minVal=new ArrayList<String>();
maxVal=new ArrayList<String>();
ImageURL=new ArrayList<String>();
Country=new ArrayList<String>();
CompanyID=new ArrayList<String>();
CompanyName=new ArrayList<String>();
FieldLevel=new ArrayList<String>();
FieldInfo=new ArrayList<String>();
if(jsonObject.has("SystemServiceID"))
{
jsonArray=jsonObject.getJSONArray("SystemServiceID");
System.out.println("Length of systemserviceID:"+jsonArray.length()+"\ngetting System ServiceID");
for(int i=0;i<jsonArray.length();i++)
{
sysSerID.add(jsonArray.getString(i));
}
}
if(jsonObject.has("SystemServiceName"))
{
jsonArray=jsonObject.getJSONArray("SystemServiceName");
System.out.println("Length of SystemServiceName:"+jsonArray.length()+"\ngetting System ServiceName");
for(int i=0;i<jsonArray.length();i++)
{
sysSerName.add(jsonArray.getString(i));
}
}
if(jsonObject.has("ProductID"))
{
jsonArray=jsonObject.getJSONArray("ProductID");
System.out.println("Length of ProductID:"+jsonArray.length()+"\ngetting ProductID");
for(int i=0;i<jsonArray.length();i++)
{
productID.add(jsonArray.getString(i));
}
}
if(jsonObject.has("ProductDescription"))
{
jsonArray=jsonObject.getJSONArray("ProductDescription");
System.out.println("Length of ProductDescription:"+jsonArray.length()+"\ngetting ProductDescription");
for(int i=0;i<jsonArray.length();i++)
{
productDesc.add(jsonArray.getString(i));
}
}
if(jsonObject.has("BatchID"))
{
jsonArray = jsonObject.getJSONArray("BatchID");
System.out.println("length of BatchID:"+jsonArray.length()+"\nGetting BatchID");
for(int i=0;i<jsonArray.length();i++)
{
batchID.add(jsonArray.getString(i));
}
}
if(jsonObject.has("MinimumValue"))
{
jsonArray = jsonObject.getJSONArray("MinimumValue");
System.out.println("length of MinimumValue:"+jsonArray.length()+"\nGetting MinimumValue");
for(int i=0;i<jsonArray.length();i++)
{
minVal.add(jsonArray.getString(i));
}
}
if(jsonObject.has("MaximumValue"))
{
jsonArray = jsonObject.getJSONArray("MaximumValue");
for(int i=0;i<jsonArray.length();i++)
{
maxVal.add(jsonArray.getString(i));
System.out.println(jsonArray.getString(i));
}
}
if(jsonObject.has("ProductName"))
{
jsonArray=jsonObject.getJSONArray("ProductName");
System.out.println("length of productID:"+jsonArray.length()+"\nGetting product name");
for(int i=0;i<jsonArray.length();i++)
{
productName.add(jsonArray.getString(i));
}
}
/*if(jsonObject.has("ProductType"))
{
jsonArray=jsonObject.getJSONArray("ProductType");
System.out.println("length of productID:"+jsonArray.length()+"\nGetting product Type");
for(int i=0;i<jsonArray.length();i++)
{
productType.add(jsonArray.getString(i));
}
}*/
if(jsonObject.has("ImageURL"))
{
jsonArray=jsonObject.getJSONArray("ImageURL");
System.out.println("length of ImageURL:"+jsonArray.length()+"\nGetting ImageURL");
for(int i=0;i<jsonArray.length();i++)
{
ImageURL.add(jsonArray.getString(i));
}
}
if(jsonObject.has("Country"))
{
jsonArray=jsonObject.getJSONArray("Country");
System.out.println("length of ImageURL:"+jsonArray.length()+"\nGetting Country");
for(int i=0;i<jsonArray.length();i++)
{
Country.add(jsonArray.getString(i));
}
}
if(jsonObject.has("CompanyID"))
{
jsonArray=jsonObject.getJSONArray("CompanyID");
System.out.println("length of CompanyID:"+jsonArray.length()+"\nGetting CompanyID");
for(int i=0;i<jsonArray.length();i++)
{
CompanyID.add(jsonArray.getString(i));
}
}
if(jsonObject.has("CompanyName"))
{
jsonArray=jsonObject.getJSONArray("CompanyName");
System.out.println("length of CompanyName:"+jsonArray.length()+"\nGetting CompanyName");
for(int i=0;i<jsonArray.length();i++)
{
CompanyName.add(jsonArray.getString(i));
}
}
if(jsonObject.has("FieldLevel"))
{
jsonArray=jsonObject.getJSONArray("FieldLevel");
System.out.println("length of FieldLevel:"+jsonArray.length()+"\nGetting FieldLevel");
for(int i=0;i<jsonArray.length();i++)
{
FieldLevel.add(jsonArray.getString(i));
}
}
if(jsonObject.has("FieldInfo"))
{
jsonArray=jsonObject.getJSONArray("FieldInfo");
System.out.println("length of FieldInfo:"+jsonArray.length()+"\nGetting FieldInfo");
for(int i=0;i<jsonArray.length();i++)
{
FieldInfo.add(jsonArray.getString(i));
}
}
ArrayList<BillPayProduct> BillPayproductList=new ArrayList<BillPayProduct>();
for(int i=0;i<productID.size();i++)
{
BillPayProduct billpay_products=new BillPayProduct();
billpay_products.setSystemServiceID((String)sysSerID.get(i));
billpay_products.setSystemServiceName((String)sysSerName.get(i));
billpay_products.setProductID((String)productID.get(i));
billpay_products.setProductName((String)productName.get(i));
billpay_products.setProductDesc((String)productDesc.get(i));
billpay_products.setMinValue((String)minVal.get(i));
billpay_products.setMaxValue((String)maxVal.get(i));
billpay_products.setImageURL((String)ImageURL.get(i));
billpay_products.setCountry((String)Country.get(i));
billpay_products.setCompanyID((String)CompanyID.get(i));
billpay_products.setCompanyName((String)CompanyName.get(i));
billpay_products.setFieldLevel((String)FieldLevel.get(i));
billpay_products.setFieldInfo((String)FieldInfo.get(i));
//inserting product into Database.
//inserting product into ArrayList
BillPayproductList.add(billpay_products);
}
return BillPayproductList;
}
}
catch (final JSONException e)
{
return null;
}
}
豆类
public class BillPayProduct extends Products
{
String Country;
String CompanyID;
String CompanyName;
String FieldLevel;
String FieldInfo;
public String getCountry()
{
return Country;
}
public void setCountry(String country)
{
Country = country;
}
public String getCompanyID()
{
return CompanyID;
}
public void setCompanyID(String companyID)
{
CompanyID = companyID;
}
public String getCompanyName()
{
return CompanyName;
}
public void setCompanyName(String companyName)
{
CompanyName = companyName;
}
public String getFieldLevel()
{
return FieldLevel;
}
public void setFieldLevel(String fieldLevel)
{
FieldLevel = fieldLevel;
}
public String getFieldInfo()
{
return FieldInfo;
}
public void setFieldInfo(String fieldInfo)
{
FieldInfo = fieldInfo;
}
}
我的问题是我如何检查它有多少个字段???以及如何解析和存储?
存储这些值的最佳方式是什么?
任何帮助将不胜感激。
提前致谢 ... :)