gpt4 book ai didi

android - 将没有根元素的Json转换为对象?使用 Gson、Ksoap2、Json.NET

转载 作者:行者123 更新时间:2023-11-29 21:12:39 26 4
gpt4 key购买 nike

在我的 android 应用程序中,我从我的网络服务中得到了这个响应:

[
{
"bookNum":1,
"title":"Halo the fall",
"author":"Erick Nylum"
},
{
"bookNum":2,
"title":"Halo contact",
"author":"Erick Nylum"
}
]

在 android 中,我试图使用这个 json 来转换数组或列表对象,因为我没有根元素

        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是我的JSON字符串,不知道怎么转化成这个对象。

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;
}

}

我的网络服务

[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;
}

但我不能,我看到的所有方法(很多)都使用“根元素”,通常是对象名称,在我的例子中我没有。怎么了?
我关注这个video但是他的 JSON 中有一个根元素(对象名称)。

有人请给我教程或链接或示例代码吗?


非常感谢@Paresh..你成就了我的一天..在你的帮助下我仍然继续前进..实际上我创建了一个方法来接收jsonData并将数据放在listView中,然后是link ...是正确的方法吗?

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();
}
}

谢谢!

最佳答案

问题在这里:

JSONObject json= new JSONObject(jsonData);
JSONObject data= json.getJSONObject("book");//no root ERROR

解决方案:问题是您正在获取 JSONArray 作为响应,但您正在使用响应字符串创建 JSONObject。

JSONArray arrayResponse = new JSONArray(jsonData);  // correct way

现在,要获取子 JSON 对象,您必须遍历 JSONArray。

for(int i=0; i<arrayResponse.length(); i++)
{
JSONObject subObj = arrrayResponse.getJSONObject(i);
// Now you can fetch strings/int and other values from object.
}

关于android - 将没有根元素的Json转换为对象?使用 Gson、Ksoap2、Json.NET,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22334650/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com