[英]Json parsing error using Java
I am new to Java, and want to parse the JSON response below. 我是Java新手,并且想解析以下JSON响应。 I can get to ID
but not able to get to startDate
.. Need help. 我可以获取ID
但无法获取startDate
..需要帮助。
The StratDate
& EndDate
are separate KEYS under (LIST VALUES) OfferDateRange
but not sure how to define that in Java class. StratDate
和EndDate
是(LIST VALUES) OfferDateRange
下的单独键,但不确定如何在Java类中定义它。
You can check the JSON data through http://jsonviewer.stack.hu/ , I am not able to paste a picture of it. 您可以通过http://jsonviewer.stack.hu/检查JSON数据,我无法粘贴其图片。
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"
}
}
]
}
}
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 ;
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/ . 代替这个复杂的事情,试试这个http://www.json.org/java/ 。
Happy coding..!! 祝您编码愉快!!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.