Maxsteel
Maxsteel

Reputation: 2040

Json string to update TextView

I want to extract a JSON string from a database and update a textview.My Json string looks like this:

[
    {
        "meaning": "A boy or young man (often as a form of address)",
        "examples": [
            "I read that book when I was a <em>lad</em>",
            "come in, <em>lad</em>, and shut the door"
        ]
    },
    {
        "meaning": "A group of men sharing recreational, working, or other interests",
        "examples": [
            "she wouldn't let him go out with the <em>lads</em> any more"
        ]
    },
    {
        "meaning": "A man who is boisterously macho in his behavior or actions, esp. one who is interested in sexual conquest",
        "examples": [
            "Tony was a bit of a <em>lad</em>âalways had an eye for the women"
        ]
    },
    {
        "meaning": "A stable worker (regardless of age or sex)",
        "examples": []
    }
]


I just want to extract "meaning".How can i do that?

Upvotes: 0

Views: 342

Answers (1)

MByD
MByD

Reputation: 137362

First, this is a JSON array, with many meanings inside it.

You can iterate over it like that:

JSONArray jarr = new JSONArray(theJsonString);
for (int i = 0; i < jarr.length(); ++i)
{
    JSONObject jCell = jarr.getJSONObject(i);
    String meaning = cell.getString("meaning");
    // set it as text? concatenate the strings?
}

Note:

  • To keep the code clear, I didn't handle exceptions, do it in your code.
  • Read more about the JSON format.
  • The following classes are part of android: JSONArray, JSONObject.
  • Worth reading, next time you won't have to ask :)

Upvotes: 2

Related Questions