当前位置: 首页 > 知识库问答 >
问题:

解析JSON JAVA中的复杂字符串[重复]

仇和蔼
2023-03-14

我有一个非常复杂的字符串,如下所示,

"data":"[
         {
           "id": "123456",
           "from": 
            {
               "name": "ABC",
               "id": "123"
             },

            "message": "isafh",
            "story": "Best Story",
            "properties": 
            [
             {
               "name": "By",
               "text": "PROUD TO BE AN INDIAN",
               "href": "www.xyz.com"
             }
           ],
           "privacy": 
           {
                      "value": ""
           },
           "type": "photo",
           "created_time": "2013-10-24T07:17:28+0000",
           "updated_time": "2013-10-24T07:17:28+0000"
          },
          {
           "id": "122423456",
            "from": 
             {
                "name": "ABCaasd",
                "id": "124233"
              },

             "message": "isafh",
             "story": "Best nice Story",
             "properties": 
             [
              {
                "name": "By",
                "text": "PROUD TO BE AN INDIAN",
                "href": "www.abc.com"
              }
            ],
            "privacy": 
            {
                       "value": ""
            },
           "type": "photo",
           "created_time": "2013-10-24T07:17:28+0000",
         },
         {
           Similar data as above {... }
         },
       ]"
"next token":"1233"

这里所有的JSON数据都在括号“[]”中,括号之间用“{…}”分隔支撑。在这里,我想要一个从所有花括号的消息,故事和属性。尝试了两件事一是二把所有的东西都放在一个JSON对象中,也尝试了一次无用的尝试来匹配regex“message:”但即使这样也没用。

从所有大括号中查找消息、故事和属性的方法是什么。

共有2个答案

樊杰
2023-03-14

使用像google gson这样的json库https://code.google.com/p/google-gson/

这是一个用gson-1.7.1.jar测试的工作示例,但它也应该适用于最新版本。

public static void main(String[] args) {
    String jsonData = "{\"data\":[{\"id\": \"123456\",\"from\": {\"name\": \"ABC\",\"id\": \"123\"},\"message\": \"isafh\",\"story\": \"Best Story\",\"properties\": [{\"name\": \"By\",\"text\": \"PROUD TO BE AN INDIAN\",\"href\": \"www.xyz.com\"}],\"privacy\": {\"value\": \"\"},\"type\": \"photo\",\"created_time\": \"2013-10-24T07:17:28+0000\",\"updated_time\": \"2013-10-24T07:17:28+0000\"},{\"id\": \"122423456\",\"from\": {\"name\": \"ABCaasd\",\"id\": \"124233\"},\"message\": \"isafh\",\"story\": \"Best nice Story\",\"properties\": [{\"name\": \"By\",\"text\": \"PROUD TO BE AN INDIAN\",\"href\": \"www.abc.com\"}],\"privacy\": {\"value\": \"\"},\"type\": \"photo\",\"created_time\": \"2013-10-24T07:17:28+0000\"}],\"next token\":\"1233\"}";
    List<String> messages = parse(jsonData);
    for (String message : messages) {
        System.out.println(message);
    }
}

public static List<String> parse(String jsonData) {
    List<String> messages = new ArrayList<String>();
    JsonElement jsonElement = new JsonParser().parse(jsonData);
    JsonObject jsonTopObject = jsonElement.getAsJsonObject();
    JsonArray jsonArray = jsonTopObject.getAsJsonArray("data").getAsJsonArray();
    Iterator<JsonElement> iterator = jsonArray.iterator();
    while (iterator.hasNext()) {
        JsonObject jsonObject = iterator.next().getAsJsonObject();
        messages.add(jsonObject.get("message").getAsString());
    }
    return messages;
}

请注意,您提供的json数据为了有效,有一些小的变化。例如,包裹在 "{","}" 形成一个json对象,也在数据的末尾你有"created_time":"2013-10-24T07:17:28 0000 ",},所以最后一个昏迷已被删除。

璩浩广
2023-03-14

正则表达式不适合解析JSON或HTML文档等复杂数据结构。幸运的是,有一些快速、可靠和免费的图书馆可以很好地完成这项工作。

参见json。org的Java解决方案,或者可以解析JSON中列出的JSON的其他库的列表。org(滚动到页面底部)。

使用json.org的库解析JSON文档非常简单:

String json = "{ \"message\" : \"Hi, this is the value for the key \\\"message\\\"\" }";
JSONObject myObject = new JSONObject(json); // This line actually parses the JSON text
System.out.println(myObject.getString("message"));
 类似资料: