如何在Java中读取Json字符串

时间:2014-08-14 12:57:12

标签: java json

我需要从json集成一些数据,我能够将json文件保存为String,但我无法阅读,我需要表单。 从下面的json我需要获取firstname和lastname将它保存在我的类型为Contact的java对象上。

{
  "form-submissions": [
    {
      "portal-id": 62515.0,
      "page-title": "Testing!",
      "title": "Test Form",
      "timestamp": 1.33830544E12,
      "conversion-id": "bc65112f47c2469e90f5951213a66110",
      "page-url": "http: //www.example.com/test/Default.aspx?RewriteStatus\u003d1",
      "page-id": "807831",
      "form-id": "4320"
    }
  ],
  "identity-profiles": [
    {
      "identities": [
        {
          "timestamp": 1.33830544E12,
          "type": "LEAD_GUID",
          "value": "8a5c2ed0379938020137993893d40013"
        },
        {
          "timestamp": 1.33830544E12,
          "type": "EMAIL",
          "value": "test@example.com"
        }
      ],
      "vid": 71.0
    },
    {
      "identities": [],
      "vid": 59.0
    }
  ],
  "properties": {
    "lifecyclestage": {
      "value": "lead",
      "versions": [
        {
          "timestamp": 1.338305450005E12,
          "selected": false,
          "source-label": "None",
          "value": "lead",
          "source-type": "MIGRATION",
          "source-id": "None"
        }
      ]
    },
    "hs_analytics_first_timestamp": {
      "value": "1337815228837",
      "versions": [
        {
          "timestamp": 1.337817901004E12,
          "selected": false,
          "source-label": "None",
          "value": "1337815228837",
          "source-type": "ANALYTICS",
          "source-id": "None"
        }
      ]
    },
    "hs_analytics_source_data_1": {
      "value": "",
      "versions": [
        {
          "timestamp": 1.337817901004E12,
          "selected": false,
          "source-label": "None",
          "value": "",
          "source-type": "ANALYTICS",
          "source-id": "None"
        }
      ]
    },
    "firstname": {
      "value": "Test",
      "versions": [
        {
          "timestamp": 1.338305440003E12,
          "selected": false,
          "source-label": "FirstName",
          "value": "Test",
          "source-type": "FORM",
          "source-id": "bc65112f47c2469e90f5951213a66110"
        }
      ]
    },
    "hs_analytics_last_url": {
      "value": "http: //www.example.com/test/",
      "versions": [
        {
          "timestamp": 1.337817901004E12,
          "selected": false,
          "source-label": "None",
          "value": "http: //www.example.com/test/",
          "source-type": "ANALYTICS",
          "source-id": "None"
        }
      ]
    },
    "hs_analytics_source_data_2": {
      "value": "",
      "versions": [
        {
          "timestamp": 1.337817901004E12,
          "selected": false,
          "source-label": "None",
          "value": "",
          "source-type": "ANALYTICS",
          "source-id": "None"
        }
      ]
    },
    "lastname": {
      "value": "Contact",
      "versions": [
        {
          "timestamp": 1.338305440003E12,
          "selected": false,
          "source-label": "LastName",
          "value": "Contact",
          "source-type": "FORM",
          "source-id": "bc65112f47c2469e90f5951213a66110"
        }
      ]
    },
    "createdate": {
      "value": "1338305440000",
      "versions": [
        {
          "timestamp": 1.338305440003E12,
          "selected": false,
          "source-label": "None",
          "value": "1338305440000",
          "source-type": "FORM",
          "source-id": "bc65112f47c2469e90f5951213a66110"
        }
      ]
    },
    "hs_analytics_num_page_views": {
      "value": "3",
      "versions": [
        {
          "timestamp": 1.337817901004E12,
          "selected": false,
          "source-label": "None",
          "value": "3",
          "source-type": "ANALYTICS",
          "source-id": "None"
        }
      ]
    },
    "hs_analytics_last_timestamp": {
      "value": "1337815238097",
      "versions": [
        {
          "timestamp": 1.337817901004E12,
          "selected": false,
          "source-label": "None",
          "value": "1337815238097",
          "source-type": "ANALYTICS",
          "source-id": "None"
        }
      ]
    },
    "phone": {
      "value": "555-555-2262",
      "versions": [
        {
          "timestamp": 1.338305440003E12,
          "selected": false,
          "source-label": "Phone",
          "value": "555-555-2262",
          "source-type": "FORM",
          "source-id": "bc65112f47c2469e90f5951213a66110"
        }
      ]
    },
    "hs_analytics_last_referrer": {
      "value": "http: //www.example.com/test",
      "versions": [
        {
          "timestamp": 1.337817901004E12,
          "selected": false,
          "source-label": "None",
          "value": "http: //www.example.com/test",
          "source-type": "ANALYTICS",
          "source-id": "None"
        }
      ]
    },
    "field_text_7": {
      "value": "62515",
      "versions": [
        {
          "timestamp": 1.338305440003E12,
          "selected": false,
          "source-label": "HubSpotPortal(Hub)ID",
          "value": "62515",
          "source-type": "FORM",
          "source-id": "bc65112f47c2469e90f5951213a66110"
        }
      ]
    },
    "hs_analytics_first_referrer": {
      "value": "",
      "versions": [
        {
          "timestamp": 1.337817901004E12,
          "selected": false,
          "source-label": "None",
          "value": "",
          "source-type": "ANALYTICS",
          "source-id": "None"
        }
      ]
    },
    "hs_analytics_first_url": {
      "value": "http: //www.example.com/support/",
      "versions": [
        {
          "timestamp": 1.337817901004E12,
          "selected": false,
          "source-label": "None",
          "value": "http: //www.example.com/support/",
          "source-type": "ANALYTICS",
          "source-id": "None"
        }
      ]
    },
    "hs_analytics_num_visits": {
      "value": "1",
      "versions": [
        {
          "timestamp": 1.337817901004E12,
          "selected": false,
          "source-label": "None",
          "value": "1",
          "source-type": "ANALYTICS",
          "source-id": "None"
        }
      ]
    },
    "field_text_9": {
      "value": "test",
      "versions": [
        {
          "timestamp": 1.338305440003E12,
          "selected": false,
          "source-label": "CustomFormQuestion",
          "value": "test",
          "source-type": "FORM",
          "source-id": "bc65112f47c2469e90f5951213a66110"
        }
      ]
    },
    "field_text_8": {
      "value": "about20",
      "versions": [
        {
          "timestamp": 1.338305440003E12,
          "selected": false,
          "source-label": "HowManyHubSpotUsers?",
          "value": "about20",
          "source-type": "FORM",
          "source-id": "bc65112f47c2469e90f5951213a66110"
        }
      ]
    },
    "ipaddress": {
      "value": "71.172.15.18",
      "versions": [
        {
          "timestamp": 1.338305440003E12,
          "selected": false,
          "source-label": "None",
          "value": "71.172.15.18",
          "source-type": "FORM",
          "source-id": "bc65112f47c2469e90f5951213a66110"
        }
      ]
    },
    "email": {
      "value": "test@example.com",
      "versions": [
        {
          "timestamp": 1.338305440003E12,
          "selected": false,
          "source-label": "Email",
          "value": "test@example.com",
          "source-type": "FORM",
          "source-id": "bc65112f47c2469e90f5951213a66110"
        }
      ]
    },
    "hs_analytics_source": {
      "value": "DIRECT_TRAFFIC",
      "versions": [
        {
          "timestamp": 1.337817901004E12,
          "selected": false,
          "source-label": "None",
          "value": "DIRECT_TRAFFIC",
          "source-type": "ANALYTICS",
          "source-id": "None"
        }
      ]
    }
  },
  "vid": 71.0
}

我试图使用com.google.gson.Gson;直接匹配价值,但没有运气:

CrmContacts contact = gson.fromJson(jsonString, CrmContacts.class);

CrmContacts类:

public class CrmContacts {
    private String lastname;
    private String firstname;

    public String getLastname() {
        return lastname;
    }
    public void setLastname(String lastname) {
        this.lastname = lastname;
    }
    public String getFirstname() {
        return firstname;
    }
    public void setFirstname(String firstname) {
        this.firstname = firstname;
    }
}

1 个答案:

答案 0 :(得分:0)

您需要在Java POJO类或Map

中创建JSON字符串的精确副本

以下是从firstname

获取lastnameproperties的简单代码
Type type = new TypeToken<Map<String, Object>>() {}.getType();

// JSON string to Java Map object
Map<String, Object> data = new Gson().fromJson(jsonString, type);
Map<String,Object> properties=(Map<String,Object>)data.get("properties");

Object lastname = properties.get("lastname");
Object firstname = properties.get("firstname");

// convert Object into JSON string back
System.out.println(new Gson().toJson(lastname));

您需要花一些时间阅读GSON library