如何从JsonArray解析数组?

时间:2015-02-21 16:45:11

标签: java arrays json

我有像

这样的JSON数组
"Headlines": [
        {
            "Title": "SPYRUS Announces Support for Windows To Go 10 Preview",
            "Date": "2/18/2015",
            "UTCOffset": 0,
            "Source": "Marketwire - News Releases",
            "Url": "http://www.redinews.com/news/?story=201502151176008.html",
            "Images": [],
                "Tags": [
                    {
                        "TagType": "Symbols",
                        "TagValues": [
                            "MSFT"
                        ]
                    },
                    {
                        "TagType": "Companies",
                        "TagValues": [
                            "Microsoft Corporation"
                        ]
                    },

我使用org.json.simple.parser.JSONParser

使用此代码访问“标题”
 JSONParser jsonParser = new JSONParser();
 JSONObject jsonObject = (JSONObject) jsonParser.parse(obj);
 JSONArray lang = (JSONArray) jsonObject.get("Headlines");

 System.out.println(lang);

但我需要访问“Tags”子数组并获取TagType和TagValues

如何访问它? 也很好也知道如何使用org.codehaus.jackson解析子数组。

由于

3 个答案:

答案 0 :(得分:1)

请参阅此link

它解释了如何迭代json数组

代码应该是这样的

JSONArray headlines = (JSONArray) jsonObject.get("Headlines");
    Iterator<String> iterator = headlines.iterator();
    while (iterator.hasNext()) {
        System.out.println(iterator.next());
    }

答案 1 :(得分:1)

检查

JSONParser jsonParser = new JSONParser();
 JSONObject jsonObject = (JSONObject) jsonParser.parse(obj);
 JSONArray lang = (JSONArray) jsonObject.get("Headlines");

JSONArray tag = lang.getJSONArray("tag");  

for (int i = 0; i < tag .length(); i++) {
        JSONObject jsonobject = jsonarray.getJSONObject(i);
        JSONArray temp= jsonobject .getJSONArray("TagValues"); 

        String tagValue = temp.getString(0);
        String tagType = jsonobject.getString("TagType");

    }

答案 2 :(得分:0)

这是一个真正有效的代码,使用简单的json和org.codehaus.jackson togheter。

JSONParser jsonParser = new JSONParser();
                            JSONObject jsonObject = (JSONObject) jsonParser.parse(obj);
                            JSONArray lang = (JSONArray) jsonObject.get("Headlines");
                            for (int i = 0; i < lang.size(); i++) {
                                JSONObject jsonobject = (JSONObject) lang.get(i);
                                Object subArray = jsonobject.get("Tags");
                                ObjectMapper mapperNew = new ObjectMapper();
                                JsonFactory factoryNew = mapperNew.getJsonFactory();
                                JsonParser jpNew;
                                System.out.println("sub Array " + subArray.toString());
                                jpNew = factoryNew.createJsonParser(subArray.toString());
                                JsonNode inputNew = mapperNew.readTree(jpNew);
                                 for (final JsonNode elementNew : inputNew) {
                                      Iterator<Map.Entry<String, JsonNode>> nodeIterator3 = elementNew.getFields();
                                       while (nodeIterator3.hasNext()) {
                                              Map.Entry<String, JsonNode> entry3 = (Map.Entry<String, JsonNode>) nodeIterator3.next();
                                               if (entry3.getKey() != null && entry3.getKey().equals("TagType")) {
                                                      System.out.println("TagType "  + entry3.getValue());
                                                 }
                                                 if (entry3.getKey() != null && entry3.getKey().equals("TagValues")) {
                                                      System.out.println("TagValues " + entry3.getValue());
                                                 }
                                       }
                                 }