是否可以根据搜索参数从JSON返回特定数据?

时间:2014-02-20 10:10:48

标签: json magento

我有以下JSON响应,我想知道Java中是否有一种方法可以根据我传递的参数返回属性。

我正在拨打当地的magentohost(/ magento / api / rest / products)来获取以下信息:

    {
            "productList": {
                "16": {
                "entity_id": "16",
                "attribute_set_id": "38",
                "type_id": "simple",
                "sku": "n261    0",
                "model": "2610",
                "dimension": "4.1 x 1.7 x 0.7 inches ",
                "description": "The Nokia 2610 is an easy to use device that combines multiple messaging options including email, instant messaging, and more. You can even download MP3 ringtones, graphics, and games straight to the phone, or surf the Internet with Cingular's MEdia Net service. It's the perfect complement to Cingular service for those even remotely interested in mobile Web capabilities in an affordable handset.<br><br>\r\n\r\n<b>Design</b><br>\r\nCompact and stylish, the 2610 features a candybar design sporting a bright 128 x 128 pixel display capable of displaying over 65,000 colors. Most of the phone's features and on-screen menus are controlled by a center toggle on the control pad. A standard hands-free headphone jack is provided, as are volume control keys, and there's even a \"Go-To\" button that can be assigned by the user for quick access to favorite applications. Lastly, the included speakerphone allows you to talk handsfree, and because the phone sports an internal antenna, there's nothing to snag or break off.\r\n\r\n",
                "in_depth": "<ul>\r\n<ul class\"disc\">\r\n<li>Integrated camera with video recorder to capture those special moments<br></li>\r\n<li>Enriched data connections for complete mobile access via Email, MMS, and MEdia Net<br></li>\r\n<li> Personalize with downloadable MP3 and polyphonic Ring tones, Games, and Graphics<br></li>\r\n<li>Use AIM, Yahoo! and MSN Messenger to stay in touch on the go<br></li>\r\n<li>Mobile Internet browser and email</li>\r\n</ul>",
                "activation_information": "Conditional $250 Equipment Discount Included: Your price paid includes an equipment discount of $250 that has been provided to you in exchange for either activating a new, non-substitute line of service or renewing an existing line of service with AT&T and your agreement that for the 181-day period following such activation or renewal you will: (1) pay your balance due to AT&T each month and otherwise maintain your account in good standing; (2) not disconnect this AT&T line of service; (3) not transfer this equipment to another AT&T line of service; (4) not change your AT&T service rate plan to a lower monthly service rate--this includes canceling or removing required PDA, BlackBerry, or smartphone features after your product has shipped; (5) not use this line of service to replace an existing account with AT&T. If these conditions are not met, you hereby authorize Magento to charge your credit card $250 as reimbursement of this equipment discount without need for further approval.",
                "meta_keyword": "Nokia 2610, cell, phone, ",
                "short_description": "The words \"entry level\" no longer mean \"low-end,\" especially when it comes to the Nokia 2610. Offering advanced media and calling features without breaking the bank",
                "custom_layout_update": "",
                "status": "1",
                "tax_class_id": "2",
                "color": "24",
                "visibility": "4",
                "weight": "3.2000",
                "price": "149.9900",
                "cost": "20.0000",
                "name": "Nokia 2610 Phone",
                "manufacturer": "20",
                "url_key": "nokia-2610-phone",
                "meta_title": "Nokia 2610",
                "meta_description": "Offering advanced media and calling features without breaking the bank, The Nokia 2610 is an easy to use",
                "gift_message_available": "",
                "options_container": "container2",
                "custom_design": ""
    },
    "17": {
            "entity_id": "17",
            "attribute_set_id": "38",
            "type_id": "simple",
            "sku": "bb8100",
            "model": "8100",
            "dimension": "4.2 x 2 x 0.6 inches ",
            "description": " Like the BlackBerry 7105t, the BlackBerry 8100 Pearl is \r\nThe BlackBerry 8100 Pearl sports a large 240 x 260 screen that supports over 65,000 colors-- plenty of real estate to view your e-mails, Web browser content, messaging sessions, and attachments. The venerable BlackBerry trackwheel has been replaced on this model with an innovative four-way trackball placed below the screen. On the rear of the handheld, you'll find a 1.3-megapixel camera and a self portrait mirror. The handheld's microSD memory card slot is located inside the device, behind the battery. There's also a standard 2.5mm headset jack that can be used with the included headset, as well as a mini-USB port for data connectivity.",
            "in_depth": "<ul>\r\n<ul class=\"disc\">\r\n<li> 1.3 mega pixel camera to capture those special moments<br></li>\r\n<li> MP3 player lets you listen to your favorite music on the go<br></li>\r\n<li>Menu and escape keys on the front of the device for easier access<br></li>\r\n<li> Bluetooth technology lets you experience hands free and wire free features<br></li>\r\n<li>Package Contents: phone,AC adapter,software CD,headset,USB cable,sim- card,get started poster,reference guide<br></li>\r\n</ul>",
            "activation_information": "CONDITIONAL $250 Equipment Discount Included: Your price paid includes an Equipment Discount of $250 that has been provided to you in exchange for activating a new, non-substitute line of service with T-Mobile and your agreement that for the 181-day period following such activation you will (1) pay your balance due to T-Mobile each month and otherwise maintain your account in good standing, (2) not disconnect this T-Mobile line of service, (3) not transfer this equipment to another T-Mobile line of service, (4) not change your T-Mobile service rate plan to a lower monthly service rate--this includes canceling or removing required PDA, BlackBerry, or smartphone features after your product has shipped, (5) not use this line of service to replace an existing account with T-Mobile. If these conditions are not met, you hereby authorize Magento.com to charge your credit card $250 as reimbursement of this Equipment Discount without need for further approval.",
            "meta_keyword": "Blackberry, 8100, pearl, cell, phone",
            "short_description": "The BlackBerry 8100 Pearl is a departure from the form factor of previous BlackBerry devices. This BlackBerry handset is far more phone-like, and RIM's engineers have managed to fit a QWERTY keyboard onto the handset's slim frame.",
            "custom_layout_update": "",
            "color": "23",
            "status": "1",
            "tax_class_id": "2",
            "visibility": "4",
            "weight": "15.2000",
            "price": "349.9900",
            "cost": "29.9900",
            "name": "BlackBerry 8100 Pearl",
            "manufacturer": "21",
            "url_key": "blackberry-8100-pearl",
            "meta_title": "BlackBerry 8100 Pearl",
            "meta_description": "BlackBerry 8100 Pearl sports a large 240 x 260 screen that supports over 65,000 colors-- plenty of real estate to view your e-mails, Web browser content, messaging sessions, and attachments.",
            "gift_message_available": "",
            "options_container": "container2",
            "custom_design": ""
    }
}
}

作为一个例子,我希望能够返回所有颜色代码为24且价格等于或大于150的产品。我(尝试)实现此目的的代码如下:

public void prepareData(float priceRange, String colour) {

    ProductListMapping productListMapping = requestAllProductsFromApi();
    Map<String, Product> map = productListMapping.getProductList();
    Set<String> keys = map.keySet();
    List<String> productNames = new ArrayList<String>();
    Iterator<String> iterator = keys.iterator();


    while (iterator.hasNext()) {
        String key = iterator.next();
        System.out.println(map.get(key).getEntity_id()+ " " +map.get(key).getName()+ " " +map.get(key).getColor()+ " " +map.get(key).getPrice());

        if (((map.get(key).getPrice() >= (priceRange)) && 
            (map.get(key).getColor() == (colourCode)))){                
               productNames.add(map.get(key).getName());
        }else{
             System.out.println("The product was not found");
             }

         }
}

0 个答案:

没有答案