jane
jane

Reputation: 241

extract from JSON

"biodata": {
    "Ruby": {
      "Expertise": "web development",
      "EXperience": "5 years"
    },
    "Dylon": {
      "Expertise": "Java",
      "EXperience": "2 years"
    }
 }

I have the above JSONObject . I am trying to fetch some keys here .

  1. I am looking to fetch the name key i.e Ruby , Dylon etc .
  2. I am then trying to fetch the "Experience " key value .

Desired output :

name= Ruby
Experience = 5 years

My code :

JSONParser parser = new JSONParser();

Object obj = parser.parse(new FileReader("path to JSON file"));

JSONObject jsonobj = (JSONObject) obj;

String statistics = jsonobj.getString("biodata"); //The method getString(String) is undefined for the type JSONObject

for (Iterator key = jsonobj.keys(); itr.hasNext();) {//The method keys() is undefined for the type JSONObject //itr cannot be resolved
     JSONObject name = jsonobj.get(key.next()); //Type mismatch: cannot convert from Object to JSONObject		
     String key = key.next();//The method next() is undefined for the type String
     JSONObject name = jsonobj.get(key); //Type mismatch: cannot convert from Object to JSONObject
     Log.d("data", "key="+key+ " and value="+jsonobj.toString()); //Log cannot be resolved		
}

I have mentioned the errors in the comment of my code .

Upvotes: 1

Views: 410

Answers (2)

KeLiuyue
KeLiuyue

Reputation: 8237

You json is not valid .

You should change to this .

{ 
 "biodata": {
    "Ruby": {
      "Expertise": "web development",
      "EXperience": "5 years"
    },
    "Dylon": {
      "Expertise": "Java",
      "EXperience": "2 years"
    }
 }
}

Try this .

private void jsonParse() {
    try {
        // use jsonobject to parse json with 
        JSONParser parser = new JSONParser();

        Object obj = parser.parse(new FileReader("path to JSON file"));

        JSONObject jsonObject = (JSONObject) obj;

        // get jsonobject by biodata tag
        JSONObject biodata = jsonObject.getJSONObject("biodata");
        // use Iterator to get name 
        Iterator<String> names = biodata.keys();
        // use while loop
        while (names.hasNext()) {
            // get name 
            String name = names.next().toString();
            Log.d("data", "name=" + name);
            // get jsonobject by name tag 
            JSONObject nameJsonObject = biodata.getJSONObject(name);
            // get string 
            String Expertise = nameJsonObject.getString("Expertise");
            String EXperience = nameJsonObject.getString("EXperience");
            Log.d("data", "Experience =" + EXperience);
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
}

Upvotes: 1

mhasan
mhasan

Reputation: 3709

You have many issues in your code.

First: Assuming you want to implement code based on your current JSON String

Issues:

  1. JSONObject API do not facilitate the methods of Map used in your implementation.

  2. Your JSON String is not a array so for loop is not going to help, instead you should fetch the inner JSONObjects using the getJSONObject () method of the JSONObject API.

  3. Casting the parsed object from your parser API will not automatically make it a JSONObject, the right way of doing this as below:

    JsonObject jsonObject = parser.parse(new FileReader("path to JSON file")).getAsJsonObject();

Second: Assuming you intent to implement the JSON string representation as array, you should correct your JSON string as below.

"biodata": [{ "Ruby": { "Expertise": "web development", "EXperience": "5 years" }}, {"Dylon": { "Expertise": "Java", "EXperience": "2 years" } }]

With the above JSON string you can implement the fetching of data logic using JSONArray API

Upvotes: 1

Related Questions