Saturday, 10 August 2013

Android json parse

Android json parse

I am going to need a little help here as with a little bit of advice. I
need an extra set of eyes to look my code and to see if there is an error
with the Yahoo Answers JSON data that I have parsed. I think I have parsed
the JSON data incorrectly and I just want somebody to look over it to make
sure I have the right data parsed.
This is the data I am trying to parse from my web service:
"Question": {
"Subject": "What gives some humans the idea that you cannot prove a
negative?",
"Content": "Seriously, humans, what is this.....",
"Date": "2010-02-07 23:18:59",
"Timestamp": "1265584739",
"Link":
"http://answers.yahoo.com/question/?qid=20100207231859AAZVura",
"Category": {
"id": "396545163",
"content": "Religion & Spirituality"
},
"UserId": "v2w0u1XAaa",
"UserNick": "Yellow Alarm Clock",
"UserPhotoURL":
"http://socialprofiles.zenfs.com/images/a3b93240efb619ea3d1b5e2e90844784_48.jpg",
"NumAnswers": "9",
"NumComments": "2",
"ChosenAnswer": "Words a clock can understand huh? Hmmm....",
"ChosenAnswererId": "FWZentXQaa",
"ChosenAnswererNick": "The CAt in the Tin Foil HAt v21",
"ChosenAnswerTimestamp": "1265585654",
"ChosenAnswerAwardTimestamp": "1265757240",
"Answers": {
"Answer": [
{
"Content": "It doesn't matter if you're talking about.....",
},
This is the way I have tried to parsed the data from the web service:
final String TAG_RESULTS = "results";
final String TAG_QUESTION_SUBJECT = "Subject";
final String TAG_QUESTION_NUMANSWERS = "NumAnswers";
final String TAG_QUESTION = "Question";
final String TAG_QUESTION_CONTENT = "Content";
final String TAG_QUESTION_CHOSENANSWER = "ChosenAnswer";
final String TAG_ANSWERS = "Answers";
final String TAG_ANSWER = "Answer";
final String TAG_ANSWERS_CONTENT = "Content";
JSONArray results;
results = jObj.getJSONArray(TAG_RESULTS);
for(int i = 0; i < results.length(); i++) {
JSONObject r = results.getJSONObject(i);
JSONObject Question = r.getJSONObject(TAG_QUESTION);
String Subject = Question.getString(TAG_QUESTION_SUBJECT);
String NumAnswers =
Question.getString(TAG_QUESTION_NUMANSWERS);
String ChosenAnswers=
Question.getString(TAG_QUESTION_CHOSENANSWER);
String Content = Question.getString(TAG_QUESTION_CONTENT);
JSONObject Answers = Question.getJSONObject(TAG_ANSWERS);
JSONObject Answer = Answers.getJSONObject(TAG_ANSWER);
String Content = Answers.getString(TAG_ANSWERS_CONTENT);
When I read the logcat is gives me an system error that says my results
string has no value. The web service is returning data to my jObj string
but not the pacific data I have parsed.

No comments:

Post a Comment