-4

How to parse JSON data in BlackBerry? Need help to parse following data.

{
    "menu": {
        "id": "Home",
        "menuitem": [
            {
                "type": "form",
                "name": "Order",
                "url": "http://domain/oredr.aspx",
                "Row": [
                    {
                        "Index": "1",
                        "Control": [
                            {
                                "type": "LB",
                                "align": "Left",
                                "send": "No",
                                "value": "User Name"
                            },
                            {
                                "type": "TB",
                                "align": "Right",
                                "send": "Yes",
                                "param": "username",
                                "maxlength": "25",
                                "ctype": "Alpha"
                            }
                        ]
                    },
                    {
                        "Index": "2",
                        "Control": [
                            {
                                "type": "LB",
                                "align": "Left",
                                "send": "No",
                                "value": "Mobile No"
                            },
                            {
                                "type": "TB",
                                "align": "Right",
                                "send": "Yes",
                                "param": "MobileNo",
                                "maxlength": "10",
                                "ctype": "Numeric"
                            }
                        ]
                    },
                    {
                        "Index": "3",
                        "Control": [
                            {
                                "type": "LB",
                                "align": "Left",
                                "send": "No",
                                "value": "Email ID"
                            },
                            {
                                "type": "TB",
                                "align": "Right",
                                "send": "Yes",
                                "param": "email",
                                "maxlength": "50",
                                "ctype": "Email"
                            }
                        ]
                    },
                    {
                        "Index": "4",
                        "Control": [
                            {
                                "type": "None",
                                "align": "Left",
                                "send": "No"
                            },
                            {
                                "type": "BT",
                                "align": "Center",
                                "send": "No",
                                "value": "Submit",
                                "ctype": "Submit"
                            }
                        ]
                    }
                ]
            },
            {
                "type": "form",
                "value": "Stock",
                "url": "http://domain/stock.aspx",
                "Row": [
                    {
                        "Index": "1",
                        "Control": [
                            {
                                "type": "LB",
                                "align": "Left",
                                "send": "No",
                                "value": "Select Medium"
                            },
                            {
                                "type": "CB",
                                "align": "Right",
                                "send": "Yes",
                                "param": "medium",
                                "Item": [
                                    {
                                        "name": "Yes"
                                    },
                                    {
                                        "name": "No"
                                    }
                                ]
                            }
                        ]
                    },
                    {
                        "Index": "2",
                        "Control": [
                            {
                                "type": "None",
                                "align": "Left",
                                "send": "No"
                            },
                            {
                                "type": "BT",
                                "align": "Center",
                                "send": "No",
                                "value": "Submit",
                                "ctype": "Submit"
                            }
                        ]
                    }
                ]
            },
            {
                "type": "form",
                "value": "Custom",
                "url": "http://domain/custom.aspx",
                "Row": [
                    {
                        "Index": "1",
                        "Control": [
                            {
                                "type": "LB",
                                "align": "Left",
                                "send": "No",
                                "value": "Offer Type"
                            },
                            {
                                "type": "DD",
                                "align": "Right",
                                "send": "Yes",
                                "param": "offertype",
                                "Item": [
                                    {
                                        "name": "Marketing"
                                    },
                                    {
                                        "name": "Promotional"
                                    }
                                ]
                            }
                        ]
                    },
                    {
                        "Index": "2",
                        "Control": [
                            {
                                "type": "None",
                                "align": "Left",
                                "send": "No"
                            },
                            {
                                "type": "BT",
                                "align": "Center",
                                "send": "No",
                                "value": "Submit",
                                "ctype": "Submit"
                            }
                        ]
                    }
                ]
            }
        ]
    }
}
Rupak
  • 3,674
  • 15
  • 23

1 Answers1

3

Refer this link first - json

Try this - First download json parser .jar file and import it into your project.

JSONObject data = new JSONObject(data);
JSONObject menu = data.getJSONObject("menu");

For getting a String value of a field, use the following code -

String id = menu.getString("id");
JSONObject menuitem = json.getJSONObject("menuitem");

and so on.

Rupak
  • 3,674
  • 15
  • 23
Rince Thomas
  • 4,158
  • 5
  • 25
  • 44