从json文件中获取用户输入检索详细信息并在java中打印

时间:2017-03-02 04:30:59

标签: java json

Data.json:

{"UniversalWord": {"UniversalWord": [
   {
      "uw_id": 1,
      "HeadWord": {"word": "aare"},
      "Restriction": {"SemanticRelations": {"feat": [
         {
            "att": "restriction_type",
            "value": "iof"
         },
         {
            "att": "target",
            "val": " "
         }
      ]}},
      "NLDescription": {
         "Gloss": {"feat": {
            "att": "Description",
            "val": "\"A RIVER IN NORTH CENTRAL SWITZERLAND THAT RUNS NORTHEAST INTO THE RHINE\""
         }},
         "Lemma": {"feat": {
            "att": "word",
            "val": "aare"
         }},
         "Example": {"feat": {
            "att": "description",
            "val": "\"\""
         }}
      },
      "MetaInfo": {
         "Frequency": {"freq": ""},
         "UWSource": {"Source_id": "WORDNET"}
      }
   },
   {
      "uw_id": 2,
      "HeadWord": {"word": "aarhus"},
      "Restriction": {"SemanticRelations": {"feat": [
         {
            "att": "restriction_type",
            "value": "iof"
         },
         {
            "att": "target",
            "val": " "
         },
         {
            "att": "restriction_type",
            "value": "equ"
         },
         {
            "att": "target",
            "val": " "
         }
      ]}},
      "NLDescription": {
         "Gloss": {"feat": {
            "att": "Description",
            "val": "\"PORT CITY OF DENMARK IN EASTERN JUTLAND\""
         }},
         "Lemma": {"feat": {
            "att": "word",
            "val": "aarhus"
         }},
         "Example": {"feat": {
            "att": "description",
            "val": "\"\""
         }}
      },
      "MetaInfo": {
         "Frequency": {"freq": ""},
         "UWSource": {"Source_id": "WORDNET"}
      }
   }
]}}

必需的输出:

Word Searched: aare
uwid = 1
headword = aare
semantic relation value = iof
target = ""
gloss = A RIVER IN NORTH CENTRAL SWITZERLAND THAT RUNS NORTHEAST INTO THE RHINE
lemma = aare
example = ""
frequency = ""
Source_ID = wordnet

code.java

public class SearchJson 
{
    public void SearchValueInJson(StringBuilder sb)
    {
        try
        {
            String jsonData = sb.toString();
            JSONObject jobj = new JSONObject(jsonData);
            Map<String,String> map = new HashMap<String,String>();
            iterateJson(jobj,map);
            System.out.println(map.toString());


        }
        catch(Exception e)
        {
            System.out.println(e);
        }

    }
     public void iterateJson(JSONObject jobj,Map map)
        {
             for(Object o : jobj.keySet())
             {
                 if(jobj.get(o.toString())instanceof JSONObject)
                     iterateJson(jobj.getJSONObject(o.toString()),map);
                 else
                     map.put(o.toString(), jobj.get(o.toString()));
             }
        }
}

这个代码我试过但它没有给我预期的输出。

如何从json文件中检索此信息?我没有得到适当的解决方案。请为此提供代码。并假设您不知道必须检索的数据的关键值。

1 个答案:

答案 0 :(得分:0)

检查以下代码段,这可能会解决您的问题。

JSONObject jobj = new JSONObject(jsonData);

        JSONArray arr = jobj.getJSONObject("UniversalWord").getJSONArray("UniversalWord");

        for (int i = 0; i < arr.length(); i++)
        {
            String uw_id = arr.getJSONObject(i).getString("uw_id");
            System.out.println(uw_id);
            String headWord = arr.getJSONObject(i).getJSONObject("HeadWord").getString("word");
            System.out.println(headWord);
            String nLDescription = arr.getJSONObject(i).getJSONObject("NLDescription").getJSONObject("Gloss").getJSONObject("feat").getString("val");
            System.out.println(nLDescription);
        }