In my android application I'm getting this response from my web service:
[
{
"bookNum":1,
"title":"Halo the fall",
"author":"Erick Nylum"
},
{
"bookNum":2,
"title":"Halo contact",
"author":"Erick Nylum"
}
]
In android I'm trying to use this json to convert in an array or list object, because I don't have a root element
btnConsumer = (Button) this.findViewById(R.id.button1);
btnConsumer.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
SoapObject request=new SoapObject(WSDL_TARGET_NAMESPACE, OPERATION_NAME);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.setOutputSoapObject(request);
envelope.dotNet=true;
HttpTransportSE httpTransport = new HttpTransportSE(SOAP_ADDRESS);// because AndroidHttpTransport now is d
try {
httpTransport.debug = true; // ?
httpTransport.call(SOAP_ACTION, envelope);
Object response = (Object) envelope.getResponse();
lblView.setText(response.toString());
String jsonData= response.toString();
JSONObject json= new JSONObject(jsonData);
JSONObject data= json.getJSONObject("book");//no root ERROR
}catch (JSONException e) {
// TODO: handle exception
e.printStackTrace();
} catch (Exception e) {
// TODO: handle exception
lblView.setText(e.toString());
// lblView.setText(e.getMessage());
}
}
});
jsonData is my JSON string, I don't know how to transform to this object.
public class books {
@SerializedName("bookNum")
private String BookNum;
@SerializedName("title")
private String Title;
@SerializedName("author")
private String Author;
public books() {
// contructor vacio
}
public books(String bookNum, String title, String author) {
super();
this.setBookNum(bookNum);
this.setTitle(title);
this.setAuthor(author);
}
public String getBookNum() {
return BookNum;
}
public String getTitle() {
return Title;
}
public String getAuthor() {
return Author;
}
public void setBookNum(String bookNum) {
BookNum = bookNum;
}
public void setTitle(String title) {
Title = title;
}
public void setAuthor(String author) {
Author = author;
}
}
My web services
[WebMethod(Description = "try return a LIST of book object in Json format using Newtonsoft")]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string ShowAllBooks()
{
string url = "Server=localhost; database=books; password=system; user=sa;";
MySqlConnection conn = new MySqlConnection(url);
conn.Open();
MySqlCommand command = conn.CreateCommand();
//select
command.CommandText = ("select * from reading");
command.Connection = conn;
MySqlDataReader reader = command.ExecuteReader();
List<Book> listaBooks = new List<Book>();
while (reader.Read())
{
Book book = new Book();
book.bookNum = Convert.ToInt16(reader.GetString(0));
book.title = reader.GetString(1).ToString();
book.author = reader.GetString(2).ToString();
listaBooks.Add(book);
}
command.Connection.Close();
//var settings = new JsonSerializerSettings();
//settings.TypeNameHandling = TypeNameHandling.Objects;
//var typebook = Type.GetType("WS_Basico.Book");
//string json = JsonConvert.SerializeObject(listaBooks,Formatting.Indented, settings);
string json = JsonConvert.SerializeObject(listaBooks, Formatting.Indented);
return json;
}
But I can't, all methods that I saw (a lot) use a "root element" normally the object name, In my case I don't have one.
What it's wrong?
I follow this video but he has a root element in his JSON (the object name).
Somebody please give me tutorial or link or sample code?
thanks a lot @Paresh.. you made my day.. With your help I still go ahead .. Actually I create a method that receive the jsonData and put the data in a listView following this link ... is the right way?
protected void ShowInView(String jsonData) {
// TODO Auto-generated method stub
JSONArray arrayResponse;
try {
arrayResponse = new JSONArray(jsonData);
for (int i = 0; i < arrayResponse.length(); i++) {
JSONObject book = arrayResponse.getJSONObject(i);
bookArrayList.add(Integer.toString(book.getInt("bookNum"))+" - "+book.getString("title"));
}
bookList.setAdapter(bookAdapter);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Thank you!