我是android的新手,正在研究如何解析Jso,所以请帮助我,我想解析这种类型的Json并希望得到“option_name”和“option_values”的值。请帮助我..我的代码是如下所示,我试过..
JSON示例
{
status: "success",
product_category: {
name: "Glossy Mannequins",
id: "28"
},
product: [
{
name: "3SC1G",
id: "128",
thumb_image: "http://3smannequins.com/uploads/product_image/mediumthumb1372498306.jpg",
big_image: "http://3smannequins.com/uploads/product_image/1400929422.jpg",
product_subcategory: {
name: "Glossy Mannequins",
id: "28"
},
product_options: [
{
option_name: "Color",
option_value: "White"
},
{
option_name: "Height",
option_value: "130 cm"
}
]
},
{
name: "3S14FG",
id: "127",
thumb_image: "http://3smannequins.com/uploads/product_image/mediumthumb1372497779.jpg",
big_image: "http://3smannequins.com/uploads/product_image/1400929548.jpg",
product_subcategory: {
name: "Glossy Mannequins",
id: "28"
},
msg_option: "not_available"
},
{
name: "3S13FG",
id: "126",
thumb_image: "http://3smannequins.com/uploads/product_image/mediumthumb1372496302.jpg",
big_image: "http://3smannequins.com/uploads/product_image/1401176927.jpg",
product_subcategory: {
name: "Glossy Mannequins",
id: "28"
},
msg_option: "not_available"
},
{
name: "3S12MG",
id: "125",
thumb_image: "http://3smannequins.com/uploads/product_image/mediumthumb1372496174.jpg",
big_image: "http://3smannequins.com/uploads/product_image/1401176821.jpg",
product_subcategory: {
name: "Glossy Mannequins",
id: "28"
},
msg_option: "not_available"
},
{
name: "3S3FG",
id: "124",
thumb_image: "http://3smannequins.com/uploads/product_image/mediumthumb1372306217.jpg",
big_image: "http://3smannequins.com/uploads/product_image/1401176740.jpg",
product_subcategory: {
name: "Glossy Mannequins",
id: "28"
},
msg_option: "not_available"
},
{
name: "3S9FG",
id: "123",
thumb_image: "http://3smannequins.com/uploads/product_image/mediumthumb1372305991.jpg",
big_image: "http://3smannequins.com/uploads/product_image/1401176646.jpg",
product_subcategory: {
name: "Glossy Mannequins",
id: "28"
},
msg_option: "not_available"
},
{
name: "3S5MG",
id: "117",
thumb_image: "http://3smannequins.com/uploads/product_image/mediumthumb1372304662.jpg",
big_image: "http://3smannequins.com/uploads/product_image/1401176538.jpg",
product_subcategory: {
name: "Glossy Mannequins",
id: "28"
},
msg_option: "not_available"
},
{
name: "3S8MG",
id: "116",
thumb_image: "http://3smannequins.com/uploads/product_image/mediumthumb1372304573.jpg",
big_image: "http://3smannequins.com/uploads/product_image/1401176461.jpg",
product_subcategory: {
name: "Glossy Mannequins",
id: "28"
},
msg_option: "not_available"
},
{
name: "3S10MG",
id: "115",
thumb_image: "http://3smannequins.com/uploads/product_image/mediumthumb1372304511.jpg",
big_image: "http://3smannequins.com/uploads/product_image/1401176396.jpg",
product_subcategory: {
name: "Glossy Mannequins",
id: "28"
},
msg_option: "not_available"
},
{
name: "M12G",
id: "99",
thumb_image: "http://3smannequins.com/uploads/product_image/mediumthumb1368463047.jpg",
big_image: "http://3smannequins.com/uploads/product_image/1401180875.jpg",
product_subcategory: {
name: "Glossy Mannequins",
id: "28"
},
msg_option: "not_available"
},
{
name: "M10G",
id: "98",
thumb_image: "http://3smannequins.com/uploads/product_image/mediumthumb1368462841.jpg",
big_image: "http://3smannequins.com/uploads/product_image/1401180931.jpg",
product_subcategory: {
name: "Glossy Mannequins",
id: "28"
},
msg_option: "not_available"
},
{
name: "M8G",
id: "97",
thumb_image: "http://3smannequins.com/uploads/product_image/mediumthumb1368462735.jpg",
big_image: "http://3smannequins.com/uploads/product_image/1401180955.jpg",
product_subcategory: {
name: "Glossy Mannequins",
id: "28"
},
msg_option: "not_available"
},
{
name: "M5G",
id: "96",
thumb_image: "http://3smannequins.com/uploads/product_image/mediumthumb1368462593.jpg",
big_image: "http://3smannequins.com/uploads/product_image/1401181016.jpg",
product_subcategory: {
name: "Glossy Mannequins",
id: "28"
},
msg_option: "not_available"
},
{
name: "M1G",
id: "95",
thumb_image: "http://3smannequins.com/uploads/product_image/mediumthumb1368452614.jpg",
big_image: "http://3smannequins.com/uploads/product_image/1401180978.jpg",
product_subcategory: {
name: "Glossy Mannequins",
id: "28"
},
msg_option: "not_available"
},
{
name: "F13GB",
id: "93",
thumb_image: "http://3smannequins.com/uploads/product_image/mediumthumb1368451347.jpg",
big_image: "http://3smannequins.com/uploads/product_image/1401181053.jpg",
product_subcategory: {
name: "Glossy Mannequins",
id: "28"
},
msg_option: "not_available"
},
{
name: "F9GB",
id: "92",
thumb_image: "http://3smannequins.com/uploads/product_image/mediumthumb1368450963.jpg",
big_image: "http://3smannequins.com/uploads/product_image/1401181078.jpg",
product_subcategory: {
name: "Glossy Mannequins",
id: "28"
},
msg_option: "not_available"
},
{
name: "F3GB",
id: "91",
thumb_image: "http://3smannequins.com/uploads/product_image/mediumthumb1368450873.jpg",
big_image: "http://3smannequins.com/uploads/product_image/1401181131.jpg",
product_subcategory: {
name: "Glossy Mannequins",
id: "28"
},
msg_option: "not_available"
},
{
name: "C1G",
id: "90",
thumb_image: "http://3smannequins.com/uploads/product_image/mediumthumb1368450534.jpg",
big_image: "http://3smannequins.com/uploads/product_image/1401181181.jpg",
product_subcategory: {
name: "Glossy Mannequins",
id: "28"
},
msg_option: "not_available"
}
]
}
mycode的
if (jsonObj.has("product")) {
subCategories = jsonObj.getJSONArray("product");
if (subCategories != null
&& subCategories.length() != 0) {
// looping through All Contacts
for (int i = 0; i < subCategories.length(); i++) {
JSONObject c = subCategories.getJSONObject(i);
sub_id = c.getString("id");
sub_name = c.getString("name");
thumb_image = c.getString("thumb_image");
big_image = c.getString("big_image");
resultArray.add(big_image);
JSONArray optsn = c
.optJSONArray("product_options");
for (int j = 0; j < optsn.length(); j++) {
JSONObject d = optsn.optJSONObject(j);
sub_id = d.optString("option_name");
sub_name = d.optString("option_value");
System.out
.println("::::::::::::::::option name and value:::::::::::"
+ option_name
+ " "
+ option_value);
};
答案 0 :(得分:0)
试试这个..
您从 jsonObj JSONArray
获得产品 JSONObject
同样获得 product_options JSONArray
来自 c JSONObject
for (int i = 0; i < subCategories.length(); i++) {
JSONObject c = subCategories.getJSONObject(i);
sub_id = c.getString("id");
sub_name = c.getString("name");
thumb_image = c.getString("thumb_image");
big_image = c.getString("big_image");
if (c.has("product_options")) {
JSONArray product_optionsarray = c.getJSONArray("product_options");
for (int j = 0; j < product_optionsarray.length(); j++) {
JSONObject c1 = product_optionsarray.getJSONObject(j);
if (c1.has("option_name"))
String option_name = c1.getString("option_name");
if (c1.has("option_value"))
String option_value = c1.getString("option_value");
}
}
}
答案 1 :(得分:0)
试试这个
if (jsonStr != null) {
jsonObj = new JSONObject(jsonStr);
JSONArray categories = jsonObj.optJSONArray("product");
for (int i = 0; i < categories.length(); i++)
{
JSONObject category_object=categories.optJsonObject(i);
JSONArray subCategories=category_object.optJSONArray("product_options");
for (int i = 0; i < subCategories.length(); i++)
{
JSONObject c = subCategories.optJSONObject(i);
sub_id = c.optString("option_name");
sub_name = c.optString("option_value");
}
}
}
答案 2 :(得分:0)
JSONTokener jk = yourJson;
Object o = jk.nextValue();
if(o instanceof JSONObject){
JSONObject jo = (JSONObject)o;
// its an jsonobject.. use it, parse it, whatever
}else if(o instanceof JSONArray){
JSONArray ja = (JSONArray)o;
for(/** loop through your json array **/) {
}
}else{ /* not a json string or corrupted **/ }
在您的情况下,您应该循环Object并解析它。您可以使用
获取所有可用密钥(用于调试) Iterator<String> yourKeys = object.keys();
for( String s : yourKeys) System.out.println( s );