0

I have an XML web service. I want to parse this XML and I want to store in an separate textviews. The following is an XML content, and I have finished getting it in a String variable.

{
    "30_year_rate": 4.25,
    "30_year_pi": 196.78,
    "30_year_apr": 4.375,
    "20_year_rate": 4.375,
    "20_year_pi": 250.37,
    "20_year_apr": 4.5,
    "15_year_rate": 3.75,
    "15_year_pi": 290.89,
    "15_year_apr": 3.875,
    "10_year_rate": 3.625,
    "10_year_pi": 397.89,
    "10_year_apr": 3.75,
    "5_year_arm": 2.75,
    "5_year_pi": 163.3,
    "5_year_apr": 2.875,
    "7_year_arm": 3,
    "7_year_pi": 168.64,
    "7_year_apr": 3.125,
    "3_year_arm": 4,
    "3_year_pi": 190.97,
    "3_year_apr": 4.125,
    "5_year_io_arm": "N/A",
    "5_year_io_pi": "N/A",
    "5_year_io_apr": "N/A",
    "QuoteID": 1449,
    "Licensed": "N"
}

How can I parse this data? I want to convert it to a JSON object and retrieve it, or any other reasonable approach.

3
  • 1
    What now? This isn't XML but JSON. It is a JSON encoded string. What is the question here? Commented Jul 30, 2012 at 13:57
  • sir, actually iam doing an ksoap webservice to access an .net webservice and, i get the result as <string> {"year":1990,"month":04 ... <string>, i stored that above group of lines in an string variable, and now i want to parse it, how can i do this? Commented Jul 30, 2012 at 14:03
  • Do you want to re-write json parser by not using jsonobject? Commented Jul 30, 2012 at 14:04

2 Answers 2

1

If what you're getting back from the webservice is the string above, then you already have a JSON string. To create an object that can retrieve information from it, use something like JSONObject.

JSONObject object = new JSONObject(your_string_variable);
double thirtyYearRate = object.getDouble("30_year_rate");
String licensed = object.getString("Licensed");

etc.

You might (will) run into some issues where you try to pull a double from a JSON field that contains a string; i.e., the "N/A" fields above. You'll likely have to pull them out as strings and then try to parse doubles from them, and if the parsing throws an exception, you'll know it's a string.

Alternately, you could look into JSON binding with something like Jackson, which apparently runs on Android.

Sign up to request clarification or add additional context in comments.

Comments

0

To parse JSON, you could use the built in JSONObject org.json Or Json-lib if you use an old version of android.

To parse XML, use XMLPullParser. A sample can be found here: Parsing XML Data

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.