-3

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"}
      }
   }
]}}

Required output:

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()));
             }
        }
}

this code i tried but it is not giving me expected output.

How to retrieve this information from the json file? I'm not getting the proper solution for it. Please give code for this. And assume that you don't know key values of data on that basis have to retrieve.

Nishita
  • 25
  • 5
  • 2
    Have tried something? if yes, show us the code. If not try out something first. – Smit Mar 02 '17 at 04:32
  • Use one of the many json parses that are available – MadProgrammer Mar 02 '17 at 04:32
  • @MadProgrammer JSON parsing seems a little overkill if OP simply wants to print a file. – shmosel Mar 02 '17 at 04:34
  • @shmosel *"retrieving details **from** json file and print it in java"* - To me, that means parsing to find what they are looking for and print the results - but what do I know :P – MadProgrammer Mar 02 '17 at 04:35
  • No OP whats to traverse through data and print few attributes only. – Suchit kumar Mar 02 '17 at 04:36
  • This may help: http://stackoverflow.com/questions/10926353/how-to-read-json-file-into-java-with-simple-json-library – Suchit kumar Mar 02 '17 at 04:39
  • Possible duplicate of [How to parse JSON in Java](http://stackoverflow.com/questions/2591098/how-to-parse-json-in-java) – Vignesh Mar 02 '17 at 05:38
  • no i tried according to above link also but i m not able to get that how to do – Nishita Mar 02 '17 at 07:26
  • @MadProgrammer it will be more convinient for me if u provide a code that parses the file and give expected output. As i m new to json parsing it results in many issues so. – Nishita Mar 02 '17 at 07:27
  • @Smit The above code i have tried bt not getting expected output. – Nishita Mar 02 '17 at 07:30
  • @Suchitkumar no it is not duplicate one of that . you can have look to json file its different it contains nested JSONArrays and JSONObjects. Help me out for its solution – Nishita Mar 02 '17 at 07:33
  • I'm not saying that it is duplicate .i just saying have a look at that answer and try. – Suchit kumar Mar 02 '17 at 07:38
  • @Suchitkumar i had a look to it and tried it also bt not giving me proper output – Nishita Mar 02 '17 at 07:40

1 Answers1

0

Check the below code snippet, this may solve your problem.

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);
        }