how to parse this json obj android
i m facing prob in parsing this obj
开发者_StackOverflow{
"id": 1909,
"permalink": "http:some url",
"title": "Voting begins for third phase of Bihar polls",
"excerpt": "some data.",
"date": "October 27, 2010 21:23",
"tags": [
"bihar",
"india-politics"
]
}
pls tell how to read tags value how to read value of tags
Lets say "jsonString" is equal to that example string
JSONObject json = new JSONObject(jsonString);
int id = json.getInt("id");
String permalink = json.getString("permalink");
JSONArray tags = json.getJSONArray("tags");
String firstTag = tags.getString(0);
You need to catch JSONExceptions and optionally check json.has("someproperty") before grabbing data.
u can use Gson gson = new Gson(); List mylist = gson.fromJson(json, listtype);
u have to import gson jar which u can google it
In android Json classes are available, so no need to go elsewhere...
Step 1 : Init Json object with source string
JSONObject jObject = new JSONObject(srcString);
Step 2 : Init parent tag with another json object if parent tag contains array the take JosnArray else JsonObject, in ur case suppose obj is parent tag then
JSONObject data = jObject.getJSONObject("obj");
Step 3 : Now get String values
data.getString("id");
Or if array then
JSONArray dataArray = data.getJSONArray("tags");
JSONObject menuObject =dataArray.getJSONObject(0);
String firstvalue= menuObject.getString("first");
Use the JSONObject and its methods as Ian says above, however if you don't want your app to throw exceptions if any of the values are missing you can also use the 'opt' (Optional) methods, e.g.
JSONObject json = new JSONObject(jsonString);
String permalink = json.optString("permalink","");
Rather than throw an exception if 'permalink' is not found, it will return the second parameter, in this case an empty string.
精彩评论