Note that there are some explanatory texts on larger screens.

plurals
  1. POParsing a JSON Response from a .Net webservice
    primarykey
    data
    text
    <p>Just to get this out in the open I am new to JAVA, KSOAP, and JSON. So I'll try to explain this the best I can.</p> <p>A while ago I created a webservice to be consumed by Blackberry Apps that we're built using the plug in for Visual Studio. Now the project I am working on, I want to consume the same webservice for Android devices. For the most part I have the base code for the Android app done and working. Here's my problem:</p> <p>I can successfully call the webservice and get a response. I know from creating the webservice that it sends a JSON response. My problem is trying to parse through the JSON response. I have found a few examples that I have been suiting to my needs however I am hung up on one thing.</p> <p>In the JSON each element is preceeded by "anyType" which is forcing my code to return no results (Ultimately I am binding the data to an ArrayList) Here's what I get if I "getProperty(0).toString()...</p> <pre><code>anyType{Artist=anyType{TrackName=Champagne Supernova;}; </code></pre> <p>Here is the code I am using to parse the JSON Object....</p> <pre><code>SoapObject gr = (SoapObject)envelope.getResponse(); String ro = gr.getProperty(0).toString(); //Added just to see structure of response Artist_Result.add(gr.toString()); if (ro.startsWith("{")) { JSONObject JSONObj = new JSONObject(ro); Iterator&lt;String&gt; itr = JSONObj.keys(); while (itr.hasNext()) { String key = (String)itr.next(); String value = JSONObj.getString(key); //bundleResult.putString(key, value); Artist_Result.add(value); } } else if (ro.startsWith("[")) { JSONArr = new JSONArray(ro); for (int i = 0; i &lt; JSONArr.length(); i++) { JSONObj = (JSONObject)JSONArr.get(i); //bundleResult.putString(String.valueOf(i), JSONObj.toString()); Artist_Result.add(JSONObj.toString()); } } </code></pre> <p>WebService Code:</p> <pre><code> [WebMethod] [return: System.Xml.Serialization.XmlArrayItemAttribute(typeof(Artist))] public Artist[] GetArtist(string ArtistQuery) { // All the SQL Stuff Here SqlDataReader sReader; sReader = cmd.ExecuteReader(); List&lt;Artist&gt; Artists = new List&lt;Artist&gt;(); while (sReader.Read()) { Artist result = new Artist(); result.TrackName = sReader.GetString(0); Artists.Add(result); } sReader.Close(); sqlConn.Close(); return Artists.ToArray(); } public class Artist { public string TrackName; } </code></pre> <p>Sample of XML Output from a browser:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8" ?&gt; - &lt;ArrayOfArtist xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://bb.mcrcog.com/"&gt; - &lt;Artist&gt; &lt;TrackName&gt;Champagne Supernova&lt;/TrackName&gt; &lt;/Artist&gt; - &lt;Artist&gt; &lt;TrackName&gt;Don't Look Back In Anger&lt;/TrackName&gt; &lt;/Artist&gt; - &lt;Artist&gt; &lt;TrackName&gt;D'you Know What I Mean&lt;/TrackName&gt; &lt;/Artist&gt; - &lt;Artist&gt; &lt;TrackName&gt;Go Let It Out&lt;/TrackName&gt; &lt;/Artist&gt; </code></pre> <p>I have a feeling I will need to implement a Class, and Getters/Setters on the Android side. I'm just not sure how to go about doing that. Any help would be greatly appreciated!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload