您好我是非常新的Android和我的应用程序我正在使用Gson解析Json obejct我在listView中显示结果我使用下面的代码但我得到异常像
com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2
请帮帮我一个
{
"Header": {
"CutQuantity": 0,
"ETAQuantity": 0,
"IDRPrice": 229000,
"MasterId": 65639,
"Name": "VENICE SATIN DBY 21491 COL 25894",
"POQuantity": 0,
"ProductCode": "GG01054-14B",
"QtyOnHand": 332.1,
"ReservedQuantity": "332.1",
"SellingPrice": 229000,
"TotalAvlQuantity": 0,
"USDPrice": 26
},
"Batches": [{
"AvailableQty": 25.8,
"BatchNo": "A001",
"BinId": 128430,
"BinName": "Dummy-700",
"DeptId": 23,
"DeptName": "G.700",
"MaxBodyId": 128430,
"ProductCode": "GG01054-14B",
"ProductCodeBatch": "GG01054-14B-A001",
"ProductId": 65639,
"ProductName": "VENICE SATIN DBY 21491 COL 25894"
}, {
"AvailableQty": 40,
"BatchNo": "A002",
"BinId": 128433,
"BinName": "Dummy-700",
"DeptId": 23,
"DeptName": "G.700",
"MaxBodyId": 128433,
"ProductCode": "GG01054-14B",
"ProductCodeBatch": "GG01054-14B-A002",
"ProductId": 65639,
"ProductName": "VENICE SATIN DBY 21491 COL 25894"
}]
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
getAllTips(); //Getting friendArrayList data from Services
}
private void getAllTips() {
try {
JSONObject json = new JSONObject();
json.put("scancode", "GG01054-14B");
if (CommonUtilities.isNetWorkStateAvailble(MainActivity.this)) {
AsyncTaskClass task = new AsyncTaskClass(this, MainActivity.this,
json);
task.execute(ServiceUrl.GET_STOCKDETAILS_ID_URL, "1",
"");
} else {
Log.d("==========>", "There is Network Error");
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void doPostExecute(StatusObject statusObject) {
// TODO Auto-generated method stub
if (statusObject != null) {
if (statusObject.getResponseCode() == 401) {
Log.d("==========>", "Session Has been Expired");
} else if (statusObject.getResponseCode() == 200) {
handleResponseData(statusObject.getMessage());
} else {
Log.d("==========>", "Server not responding. Please try again later.");
}
} else {
Log.d("==========>", "Server not responding. Please try again later.");
}
}
// handle the response
private void handleResponseData(String result) {
Log.d("Final is==========>", result);
if (result != null) {
try {
JSONObject json = new JSONObject(result);
if (json.has("Header")) {
if (json.getString("Header").equalsIgnoreCase("null")
|| json.getString("Header") == null) {
CommonUtilities
.showToastMessage(MainActivity.this,
"Product Information unavailable with this Barcode");
} else {
System.out.println("your here");
handleTripsResponse(result);
}
} else if (json.has("error")) {
if (json.has("message")) {
CommonUtilities.showToastMessage(MainActivity.this,
"" + json.getString("message"));
}
}
} catch (JSONException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
} else {
CommonUtilities.showToastMessage(MainActivity.this,
"Product Information unavailable with this Barcode");
}
}
private void handleTripsResponse(String result) {
try {
Type type = new TypeToken<Response>() {
}.getType();
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.setDateFormat("M/d/yy hh:mm a");
Gson gson = gsonBuilder.create();
Response post = gson.fromJson(result,type);
friendArrayList.add(post);
int listSize = friendArrayList.size();
if (friendArrayList.size() != 0) {
listView = (ListView) findViewById(R.id.list);
adapter = new CustomListViewAdapter(MainActivity.this,
R.layout.list_item, friendArrayList);
listView.setAdapter(adapter);
}else{
Log.d("=======>" ,"No data Available");
}
} catch (Throwable throwable) {
System.out.println("Exception is " + throwable);
}
}
public class CustomListViewAdapter extends ArrayAdapter<Response> {
LayoutInflater inflater;
Context context;
ArrayList<Response> coridersList = new ArrayList<Response>();
public CustomListViewAdapter(Activity context, int resourceId,
ArrayList<Response> items) {
super(context,resourceId, items);
this.context = context;
inflater = context.getLayoutInflater();
this.coridersList = items;
}
@Override
public View getView(final int position, View view, ViewGroup parent) {
Response rowItem = getItem(position);
View rowView= inflater.inflate(R.layout.list_item, null, true);
TextView txtTitle = (TextView) rowView.findViewById(R.id.title);
txtTitle.setText(rowItem.getHeader().getAlerts().getDeptName());
TextView desc = (TextView) rowView.findViewById(R.id.desc);
desc.setText(rowItem.getHeader().getAlerts().getProductCode());
// ImageView imageView = (ImageView) rowView.findViewById(R.id.icon);
// imageView.setImageResource(rowItem.getImageId());
return rowView;
}
}
public class Response {
private Header header;
public Header getHeader(){
return header;
}
public void setHeader(Header header){
this.header = header;
}
}
public class Header {
private TerritoryBean alerts;
private String Name;
private String ProductCode;
private int MasterId;
private int SellingPrice;
private int IDRPrice;
private int USDPrice;
private int POQuantity;
private int ReservedQuantity;
private float ETAQuantity;
private float CutQuantity;
private float TotalAvlQuantity;
private float QtyOnHand;
public TerritoryBean getAlerts(){
return alerts;
}
public void setAlerts(TerritoryBean alerts){
this.alerts = alerts;
}
public String getName(){
return Name;
}
public void setName(String Name){
this.Name = Name;
}
public String getProductCode(){
return ProductCode;
}
public void setProductCode(String ProductCode){
this.ProductCode = ProductCode;
}
}
public class TerritoryBean {
private int DeptId;
private String DeptName;
private int ProductId;
private String ProductCode;
private String ProductName;
private String BatchNo;
private String ProductCodeBatch;
private int MaxBodyId;
private float AvailableQty;
private String BinId;
private String BinName;
public int getDeptId(){
return DeptId;
}
public void setDeptId(int DeptId){
this.DeptId = DeptId;
}
public String getDeptName(){
return DeptName;
}
public void setDeptName(String DeptName){
this.DeptName = DeptName;
}
public int getProductId(){
return ProductId;
}
public void setProductId(int ProductId){
this.ProductId = ProductId;
}
public String getProductCode(){
return ProductCode;
}
public void setProductCode(String ProductCode){
this.ProductCode = ProductCode;
}
public String getProductName(){
return ProductName;
}
public void setProductName(String ProductName){
this.ProductName = ProductName;
}
}
答案 0 :(得分:0)
我认为问题在于你的json响应你的json对象响应应该
"Header":{
CutQuantity = 0;
ETAQuantity = 0;
IDRPrice = 229000;
MasterId = 65639;
Name = "VENICE SATIN DBY 21491 COL 25894";
POQuantity = 0;
ProductCode = "GG01054-14B";
QtyOnHand = "332.1";
ReservedQuantity = "332.1";
SellingPrice = 229000;
TotalAvlQuantity = 0;
USDPrice = 26;
}
和Json数组一样
"Batches" : [
{
AvailableQty = "25.8";
BatchNo = A001;
BinId = 128430;
BinName = "Dummy-700";
DeptId = 23;
DeptName = "G.700";
MaxBodyId = 128430;
ProductCode = "GG01054-14B";
ProductCodeBatch = "GG01054-14B-A001";
ProductId = 65639;
ProductName = "VENICE SATIN DBY 21491 COL 25894";
},
{
AvailableQty = 40;
BatchNo = A002;
BinId = 128433;
BinName = "Dummy-700";
DeptId = 23;
DeptName = "G.700";
MaxBodyId = 128433;
ProductCode = "GG01054-14B";
ProductCodeBatch = "GG01054-14B-A002";
ProductId = 65639;
ProductName = "VENICE SATIN DBY 21491 COL 25894";
}
]
答案 1 :(得分:0)
您最大的问题是您的JSON看起来不正确,请将其复制并粘贴到JSON validator中,并确保其正确无误。 此外,请花一些时间来学习JSON - 通过JSON tutorial here。
另外,请检查accepted answer on this question - 它看起来像你真正可以使用的东西,特别是因为你还有/期望数组/值列表。
您的JSON应如下所示:
public static List<String> getList(List<Item> DictionaryItems)
{
UmbracoDatabase db = ApplicationContext.DatabaseContext.Database;
var select = new Sql("SELECT key FROM cmsDictionary");
List<string> DictionaryItems = db.Fetch<Item>(select);
return DictionaryItems;
}