【问题标题】:How can I get JSON values inside an iterator in Java如何在 Java 中的迭代器中获取 JSON 值
【发布时间】:2017-01-16 13:15:52
【问题描述】:

我有这个 JSON 文件 my JSON file

我希望在 Java 中在 System.Out 中显示这个 sn-p 的内容:

else if(jsonObject2.get("type").equals("link")){

            System.out.println(jsonObject2.get("tr_name"));
            System.out.println(jsonObject2.get("tr_description"));
            System.out.println(jsonObject2.get("tr_rules"));
            System.out.println(jsonObject2.get("source"));
            System.out.println(jsonObject2.get("target"));

          }

到目前为止,我可以成功获得tr_name,即LINKNAME,但由于tr_descriptiontr_rulessourcetarget 更深入,我无法访问它们。从sourcetarget 我需要得到他们的id

请问我怎样才能得到它们?

我的完整 Java 代码如下:

package jsontoxml;


import java.io.*;

import org.json.simple.parser.JSONParser;
import org.json.simple.*;
import java.util.*;


public class JacksonStreamExample {

   public static void main(String[] args) {
       JSONParser parser = new JSONParser();
     try {
        Object obj = parser.parse(new FileReader("text.json"));
        JSONObject jsonObject = (JSONObject) obj;

        //Check inside the JSON array with all graph objects
        JSONArray cells = (JSONArray) jsonObject.get("cells");
        //Check inside the JSON object with all graph objects
        Iterator<JSONObject> iterator = cells.iterator();
        while(iterator.hasNext()){
        JSONObject jsonObject2 = (JSONObject) iterator.next();
        if(jsonObject2.get("type").equals("link")){

            System.out.println(jsonObject2.get("tr_name"));
            System.out.println(jsonObject2.get("tr_description"));
            System.out.println(jsonObject2.get("tr_rules"));
            System.out.println(jsonObject2.get("source"));
            System.out.println(jsonObject2.get("target"));

          }

        }

     } catch (Exception e) {

      e.printStackTrace();

     }

  }

}

到目前为止我的系统输出是:

LINKNAME
null
null
{"id":"cae4c219-c2cd-4a4b-b50c-0f269963ca24"}
{"id":"d23133e0-e516-4f72-8127-292545d3d479"}

【问题讨论】:

    标签: java json parsing key


    【解决方案1】:

    试试下面的代码 -

                    System.out.println(jsonObject2.get("tr_name"));
    
                    System.out.println(((JSONObject) ((JSONObject) jsonObject2.get("attrs")).get(".attributes"))
                            .get("tr_description"));
                    System.out.println(
                            ((JSONObject) ((JSONObject) jsonObject2.get("attrs")).get(".attributes")).get("tr_rules"));
    
                    System.out.println(((JSONObject) jsonObject2.get("source")).get("id"));
                    System.out.println(((JSONObject) jsonObject2.get("target")).get("id"));
    

    输出 -

    LINKNAME2
    LINKDESCRIPTION2
    null
    a53898a5-c018-45c4-bd3f-4ea4d69f58ed
    e2bd21f2-508d-44b9-9f68-e374d4fa87ea
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-08-06
      • 2017-12-14
      • 2017-10-23
      • 2021-03-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-15
      相关资源
      最近更新 更多