从服务器的String接收创建JSON对象

时间:2012-02-13 15:34:18

标签: java json

我想从服务器接收JSON数据创建JSON对象作为字符串。

我从服务器接收以下JSON数据到字符串“practiceInfoByUsername”

{
    "method_name": "getMyPracticeInfo",
    "module_name": "pciLoc",
    "result_set": [
        {
            "practice_address": {
                "state_province": "GA",
                "_types": [
                    "Address"
                ],
                "county_district": "",
                "address1": "1111 Test Street",
                "address2": "",
                "city_village": "Atlanta",
                "longitude": "",
                "subregion": "",
                "postal_code": "00000",
                "township_division": "",
                "country": "USA",
                "latitude": "",
                "region": "",
                "_cls": "Address"
            },
            "ucf_fee_group_id": 0,
            "practice_type": "H",
            "practice_website": "",
            "insurance_plan_fee_group_id": 0,
            "practice_edi_vendor_id": "",
            "practice_comments": "",
            "practice_date_changed": "2011-10-14 17:48:14",
            "practice_entity_id": "E01",
            "practice_pref_use_metric": true,
            "practice_changed_by": "",
            "practice_message_on_patient_statement": "",
            "practice_fax": "",
            "practice_def_birth_year": 1930,
            "practice_id": "P01",
            "practice_ein": "",
            "practice_npi": "",
            "practice_phone": "",
            "practice_email": "",
            "practice_name": "Test Practice #1",
            "practice_depts": [
                {
                    "d_name": "Accident and emergency",
                    "d_type": "AE",
                    "d_code": "EMER",
                    "d_notes": ""
                },
                {
                    "d_name": "Cardiology",
                    "d_type": "CA",
                    "d_code": "CARD",
                    "d_notes": ""
                },
                {
                    "d_name": "Diagnostic imaging",
                    "d_type": "DI",
                    "d_code": "DIAG",
                    "d_notes": ""
                },
                {
                    "d_name": "Obstetrics and gynaecology",
                    "d_type": "OB",
                    "d_code": "OBG",
                    "d_notes": ""
                }
            ]
        }
    ],
    "rec_count": 1
}

我使用以下代码创建JSON对象

JSONObject JSO = new JSONObject(practiceInfoByUsername);

但它在Netbeans中标记为错误我的字符串名称“practiceInfoByUsername”以红色下划线

我认为我在为Netbeans配置JSON库时遇到了错误

请帮帮我

2 个答案:

答案 0 :(得分:13)

代码应为

import net.sf.json.JSONObject;
JSONObject JSO = JSONObject.fromObject(practiceInfoByUsername);

你还需要json-lib jar,可以下载here

答案 1 :(得分:1)

我建议你为json使用一个库..它们有很多。 最常用的(据我所知)是google-gson ..网上也有很好的文档和示例..

您还可以查看here ..以获取JSON工具的概述。