Java 使用android截取从url迭代JSonArray响应
这是我的JSonArrayJava 使用android截取从url迭代JSonArray响应,java,android,android-asynctask,android-volley,android-json,Java,Android,Android Asynctask,Android Volley,Android Json,这是我的JSonArray { "vendor":[ { "vendor_name":"Tapan Moharana", "vendor_description":"", "vendor_slug":"tapan", "vendor_logo":null, "contact_number":null } ], "products":[ {
{
"vendor":[
{
"vendor_name":"Tapan Moharana",
"vendor_description":"",
"vendor_slug":"tapan",
"vendor_logo":null,
"contact_number":null
}
],
"products":[
{
"name":"Massage",
"price":"5000.0000",
"image":"http:\/\/carrottech.com\/lcart\/media\/catalog\/product\/cache\/1\/image\/150x\/9df78eab33525d08d6e5fb8d27136e95\/2\/9\/29660571-beauty-spa-woman-portrait-beautiful-girl-touching-her-face.jpg"
},
{
"name":"Chicken Chilly",
"price":"234.0000",
"image":"http:\/\/carrottech.com\/lcart\/media\/catalog\/product\/cache\/1\/image\/150x\/9df78eab33525d08d6e5fb8d27136e95\/c\/h\/cheicken.jpg"
},
{
"name":"Chicken Biryani",
"price":"500.0000",
"image":"http:\/\/carrottech.com\/lcart\/media\/catalog\/product\/cache\/1\/image\/150x\/9df78eab33525d08d6e5fb8d27136e95\/placeholder\/default\/image_1.jpg"
}
]
}
这是我的java代码:
JSONObject jsono = new JSONObject(response);
JSONArray children = jsono.getJSONArray("vendor");
JSONArray childrenProducts = jsono.getJSONArray("products");
for (int i = 0; i <children.length(); i++) {
JSONObject jsonData = children.getJSONObject(i);
System.out.print(jsonData.getString("vendor_name") + "<----");
// String vendorThumbNailURL=jsonData.getString("")
//jvendorImageURL.setImageUrl(local, mImageLoader);
vendorLogo=vendorLogo+jsonData.getString("vendor_logo").trim();
jvendorImageURL.setImageUrl(vendorLogo, mImageLoader);
jvendorName.setText(jsonData.getString("vendor_name"));
jvendorAbout.setText(jsonData.getString("vendor_description"));
jvendorContact.setText(jsonData.getString("contact_number"));
System.out.print(jsonData.getString("products") + "<----");
}
for(int i=0;i<childrenProducts.length();i++){
JSONObject jsonData = childrenProducts.getJSONObject(i);
System.out.println("inside products");
System.out.print(jsonData.getString("name") + "<----dd");
}
JSONObject jsono=新的JSONObject(响应);
JSONArray children=jsono.getJSONArray(“供应商”);
JSONArray childrenProducts=jsono.getJSONArray(“产品”);
对于(inti=0;i为什么不简单地使用get解析JSON字符串
您需要首先声明类来匹配JSON响应,如下所示
public class Vendor {
private String vendor_name;
private String vendor_description;
private String vendor_slug;
private String vendor_logo;
private String contact_number;
public Vendor() {
}
public String getVendor_name() {
return vendor_name;
}
public String getVendor_description() {
return vendor_description;
}
public String getVendor_slug() {
return vendor_slug;
}
public String getVendor_logo() {
return vendor_logo;
}
public String getContact_number() {
return contact_number;
}
}
Gson gson = new Gson();
Response mResponse = gson.fromJson(jsonString, Response.class);
现在像这样声明响应
类
public class Response {
private List<Vendor> vendor;
private List<Product> products;
public Response() {
}
public List<Vendor> getVendor() {
return vendor;
}
public List<Product> getProducts() {
return products;
}
}
简单!它不会进入第二个for循环,因为在第一个for循环中有一些异常。
您的执行将被抛出到任何catch()
子句,进一步的执行将不会完成,包括第二个for循环
试着像这样贴出来:
JSONObject jsono = new JSONObject(response);
JSONArray children = jsono.getJSONArray("vendor");
JSONArray childrenProducts = jsono.getJSONArray("products");
//this will be executed now..!!
for(int i=0;i<childrenProducts.length();i++){
JSONObject jsonData = childrenProducts.getJSONObject(i);
System.out.println("inside products");
System.out.print(jsonData.getString("name") + "<----dd");
}
for (int i = 0; i <children.length(); i++) {
JSONObject jsonData = children.getJSONObject(i);
System.out.print(jsonData.getString("vendor_name") + "<----");
// String vendorThumbNailURL=jsonData.getString("")
//jvendorImageURL.setImageUrl(local, mImageLoader);
vendorLogo=vendorLogo+jsonData.getString("vendor_logo").trim();
jvendorImageURL.setImageUrl(vendorLogo, mImageLoader);
jvendorName.setText(jsonData.getString("vendor_name"));
jvendorAbout.setText(jsonData.getString("vendor_description"));
jvendorContact.setText(jsonData.getString("contact_number"));
System.out.print(jsonData.getString("products") + "<----");
}
JSONObject jsono=新的JSONObject(响应);
JSONArray children=jsono.getJSONArray(“供应商”);
JSONArray childrenProducts=jsono.getJSONArray(“产品”);
//这将立即执行。。!!
对于(int i=0;i这是我如何解决它的:
try {
JSONObject jsono = new JSONObject(response);
JSONArray children = jsono.getJSONArray("vendor");
for (int i = 0; i <children.length(); i++) {
JSONObject jsonData = children.getJSONObject(i);
System.out.print(jsonData.getString("vendor_name") + "<----");
// String vendorThumbNailURL=jsonData.getString("")
//jvendorImageURL.setImageUrl(local, mImageLoader);
vendorLogo=vendorLogo+jsonData.getString("vendor_logo").trim();
jvendorImageURL.setImageUrl(vendorLogo, mImageLoader);
jvendorName.setText(jsonData.getString("vendor_name"));
jvendorAbout.setText(jsonData.getString("vendor_description"));
jvendorContact.setText(jsonData.getString("contact_number"));
System.out.print(jsonData.getString("products") + "<----");
}
} catch (JSONException e) {
e.printStackTrace();
}
try {
JSONObject jsono = new JSONObject(response);
JSONArray childrenProducts = jsono.getJSONArray("products");
System.out.println(childrenProducts.length()+"LENGTH");
for(int i=0; i<childrenProducts.length(); i++){
JSONObject jsonData1 = childrenProducts.getJSONObject(i);
System.out.println(childrenProducts.length() + "LENGTH");
System.out.print(jsonData1.getString("name") + "<----dd");
}
} catch (JSONException e) {
e.printStackTrace();
}
}
试试看{
JSONObject jsono=新的JSONObject(响应);
JSONArray children=jsono.getJSONArray(“供应商”);
对于(int i=0;我在您的响应中children只有一个项意味着children大小为1,这就是为什么它只在e上迭代..我创建了一个单独的对象:JSONArray childrenProducts=jsono.getJSONArray(“products”);这是第二个数组,奇怪的是,这段代码没有工作..您得到的childrenProducts.length()值是多少?4是我打印儿童产品时得到的。长度()根据你的回答长度应该是3..你在jsonData中获取数据吗?如果它至少先打印数据,那么我可以继续你说的,但它根本没有打印数据..你的json字符串正确吗?你可以继续这个过程。你的循环或解析可能有问题。让GSON为你做解析。您只需从mResponse
类中获取值,然后尝试打印它们。这很容易使用,我几乎为您编写了完整的代码。即使供应商有值,它也不起作用,但我认为这可能是因为某些例外。如果您不尝试两次,这将起作用。。捕获。!!我已编辑为正确。!!这是实际问题..如果它解释了相同的问题,请接受。。!!
try {
JSONObject jsono = new JSONObject(response);
JSONArray children = jsono.getJSONArray("vendor");
for (int i = 0; i <children.length(); i++) {
JSONObject jsonData = children.getJSONObject(i);
System.out.print(jsonData.getString("vendor_name") + "<----");
// String vendorThumbNailURL=jsonData.getString("")
//jvendorImageURL.setImageUrl(local, mImageLoader);
vendorLogo=vendorLogo+jsonData.getString("vendor_logo").trim();
jvendorImageURL.setImageUrl(vendorLogo, mImageLoader);
jvendorName.setText(jsonData.getString("vendor_name"));
jvendorAbout.setText(jsonData.getString("vendor_description"));
jvendorContact.setText(jsonData.getString("contact_number"));
System.out.print(jsonData.getString("products") + "<----");
}
} catch (JSONException e) {
e.printStackTrace();
}
try {
JSONObject jsono = new JSONObject(response);
JSONArray childrenProducts = jsono.getJSONArray("products");
System.out.println(childrenProducts.length()+"LENGTH");
for(int i=0; i<childrenProducts.length(); i++){
JSONObject jsonData1 = childrenProducts.getJSONObject(i);
System.out.println(childrenProducts.length() + "LENGTH");
System.out.print(jsonData1.getString("name") + "<----dd");
}
} catch (JSONException e) {
e.printStackTrace();
}
}