0

I am new to Java, and want to parse the JSON response below. I can get to ID but not able to get to startDate.. Need help.

The StratDate & EndDate are separate KEYS under (LIST VALUES) OfferDateRange but not sure how to define that in Java class.

You can check the JSON data through http://jsonviewer.stack.hu/, I am not able to paste a picture of it.

import java.util.ArrayList;
    public class offers
    {
      private ArrayList<PkgData> pkg;

      class PkgData
      {
        Info Info;
        offerDateRange offerDateRange;


        class Info
        {
          int Id;

        }

        class offerDateRange
        {
          String StartDate;

          String EndDate;

        }

        public int getId() {
          return Info.Id;
        }

        public String getStartDate() {
          if (offerDateRange != null && offerDateRange.StartDate != null)
            return offerDateRange.StartDate;
          return "";
        }
   }

      public ArrayList<PkgData> getpkg() {
    return pkg;
  }

}


JSON:

{  
   "offerInfo":{  
      "siteID":"1",
      "language":"en_US",
      "currency":"USD"
   },
   "offers":{  
      "pkg":[  
         {  
            "offerDateRange":{  
               "StartDate":[  
                  2015,
                  11,
                  8
               ],
               "EndDate":[  
                  2015,
                  11,
                  14
               ]
            },
            "Info":{  
               "Id":"111"
            },
            "PricingInfo":{  
               "BaseRate":1932.6
            },
            "flt_Info":{  
               "Carrier":"AA"
            }
         }
      ]
   }
}
1

2 Answers 2

1

if start date and end date are two different variables .you can not get in one variable . append both date strings with comma .

   String date = startDate + "," + endDate ;
Sign up to request clarification or add additional context in comments.

Comments

0

try this

class Offers
{
    List<PkgData> pkg;
}
classs PkgData
{
    OfferDateRange offerDateRange;
    Info Info;
    PricingInfo PricingInfo ;
    Flt_Info Flt_Info;  
}
class OfferDateRange
{
    List<String> StartDate;
    List<String> EndDate;
}

Instead of this complex thing, try this http://www.json.org/java/.

Happy coding..!!

2 Comments

I am still unable to get it working with your code,it still not able to parse date fields
I think we need to handle startdate and enddate differently ..Please check the sample JSON in jsonviewer.stack.hu

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.