1

我有这样的 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;
    }
}

我的问题是我如何检查它有多少个字段???以及如何解析和存储?

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

任何帮助将不胜感激。

提前致谢 ... :)

4

3 回答 3

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

} 
于 2013-07-25T07:26:28.233 回答
0
JSONObject jsonObject = new JSONObject(JSONResponse);
String mResponseCode = jsonObject.getString("ResponseCode");
String mResponseDescription = jsonObject.getString("ResponseDescription");
JSONArray jsonArray = jsonObject.getJSONArray("SystemServiceID");

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

多谢,伙计。如果您有任何疑问,请添加评论。

于 2013-07-25T07:26:56.173 回答
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.
                            }
                        }
                    }
                }
            }
        }
    }
}

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

希望这可以帮助!:)

于 2013-07-25T07:52:37.090 回答