检查从服务器动态生成的JSON值

时间:2013-07-25 07:09:46

标签: android json

我有像这样的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,其中包含Field1,Field2是从服务器动态生成的,有时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;
        }
    }

Bean类

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;
    }
}

我的问题是我怎样才能检查它有多少个字段? 以及如何解析并存储?

存储这些值的最佳方式是什么?

任何帮助将不胜感激。

先谢谢... :)

3 个答案:

答案 0 :(得分:0)

您可以枚举根的键,例如

Iterator<String> keys = obj.keys();
  

存储这些值的最佳方式是什么?

我会创建一个只读类,其中包含我期望的值,并将其添加到集合中:

public class JSONDescriptor {
  public final String filed1;
  public final String filed2;

  public JSONDescriptor(String filed1, String filed2) {
       this.field1 = field1;
       this.field2 = field2;
  }

} 

答案 1 :(得分:0)

JSONObject jsonObject = new JSONObject(JSONResponse);
String mResponseCode = jsonObject.getString("ResponseCode");
String mResponseDescription = jsonObject.getString("ResponseDescription");
JSONArray jsonArray = jsonObject.getJSONArray("SystemServiceID");

在这个jsonArray中你只有一个值。如果是这样的话,我们就无法解析这个值。 所以,它应该像jsonArray中的这种格式"Id": "4"(这意味着对象及其值)。 否则,如果只需要一个id,则将其更改为"SystemServiceID": "4"这种格式,并将其解析为mRsespoonseCode解析技术。

谢谢老兄。如果您有任何疑问,请添加评论。

答案 2 :(得分:0)

以下是解析JSON String

的确切方法
JSONObject mMainObj = new JSONObject(mMyResult);
if(mMainObj != null){
    String response = mMainObj.getString("ResponseCode"); // gets ResponseCode
    String desc = mMainObj.getString("ResponseDescription"); //gets ResponseDescription

    // SystemServiceID
    JSONArray mSysService = mMainObj.getJSONArray("SystemServiceID");
    if(mSysService != null){
        for (int j = 0; j < mSysService.length(); j++) {
            int mId = mSysService.getInt(j); // gets 4 and all other values, if there are some
        }
    }

    // SystemServiceName
    JSONArray mSysServiceName = mMainObj.getJSONArray("SystemServiceName");
    if(mSysServiceName != null){
        for (int j = 0; j < mSysServiceName.length(); j++) {
            String mName = mSysServiceName.getString(j); // gets Test Name and all other values, if there are some
        }
    }

    // The same for ProductID, ProductName and etc.

    JSONArray mFieldInfo = mMainObj.getJSONArray("FieldInfo");
    if(mFieldInfo != null){
        for(int j = 0; j < mFieldInfo.length(); j++){
            JSONObject mItem = mFieldInfo.getJSONObject(i);
            if(mItem != null){
                Iterator<Object> keys = mItem.keys();
                while(keys.hasNext()){
                    String id = String.valueOf(keys.next()); // This value represent Field1
                    JSONArray mField = mItem.getJSONArray(id);
                    if(mField != null){
                        for(int i = 0; i < mField.length();i++){
                            JSONObject mElem = mField.getJSONObject(i);
                            if(mElem != null){
                                String mFieldName = mElem.getString("Field Name"); // value: Phone Number
                                // and etc.
                            }
                        }
                    }
                }
            }
        }
    }
}

没有测试过代码,可能会有一些拼写错误,如果它没有正常工作只是发表评论,我会修复它。

希望这有帮助! :)