gpt4 book ai didi

android - 没有键值的 JSON 序列化响应

转载 作者:行者123 更新时间:2023-11-29 02:18:30 26 4
gpt4 key购买 nike

我正在使用 retrofit 进行 http 调用,使用 gson 进行 json 解析。这是我的 json

 {
"-109.457154947154,26.4155454751248": [
{
"data": {
"date": "2017-13-13",
"source_type": "Light",
"table_value": 3,
"condition": "Good"
}
},
{
"data": {
"date": "2019-11-15",
"source_type": "Light",
"table_value": 4,
"condition": "Very good"
}
},
{
"data": {
"date": "2019-11-15",
"source_type": "Heavy",
"table_value": 3,
"condition": "Good"
}
}
],
"-110.2324214532214,27.9288762948267": [
{
"data": {
"date": "2017-13-13",
"source_type": "Light",
"table_value": 3,
"condition": "Good"
}
},
{
"data": {
"date": "2019-11-15",
"source_type": "Light",
"table_value": 4,
"condition": "Very good"
}
},
{
"data": {
"date": "2019-11-15",
"source_type": "Heavy",
"table_value": 3,
"condition": "Good"
}
}
]
}

此响应没有顶级元素的键。我想序列化这个响应。

这是我的数据对象.class

public class Data {

/*
* "data": {
"date": "2017-13-13",
"source_type": "Light",
"table_value": 3,
"condition": "Good"
}
*
* */

@SerializedName("date")
@Expose
private String date;

@SerializedName("source_type")
@Expose
private String source_type;

@SerializedName("table_value")
@Expose
private String table_value;

@SerializedName("condition")
@Expose
private String condition;

public Data(String date, String source_type, String table_value, String condition) {
this.date = date;
this.source_type = source_type;
this.table_value = table_value;
this.condition = condition;
}
// Getters and Setters
}

由于我是 Android 开发的新手,所以我无法理解如何序列化其他元素。我知道我应该编写另一个模型类来解析这个 JSON 响应。但我想不出正确的做法。

最佳答案

在您的示例中也从 jsonarray 的不同键获取此动态 json 响应的完整代码:

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

ArrayList<Data> arrayListData;
arrayListData=new ArrayList<>();

String jresponse="" +
"{\n" +
" \"-109.457154947154,26.4155454751248\":[\n" +
" {\n" +
" \"data\":{\n" +
" \"date\":\"2017-13-13\",\n" +
" \"source_type\":\"Light\",\n" +
" \"table_value\":3,\n" +
" \"condition\":\"Good\"\n" +
" }\n" +
" },\n" +
" {\n" +
" \"data\":{\n" +
" \"date\":\"2019-11-15\",\n" +
" \"source_type\":\"Light\",\n" +
" \"table_value\":4,\n" +
" \"condition\":\"Very good\"\n" +
" }\n" +
" },\n" +
" {\n" +
" \"data\":{\n" +
" \"date\":\"2019-11-15\",\n" +
" \"source_type\":\"Heavy\",\n" +
" \"table_value\":3,\n" +
" \"condition\":\"Good\"\n" +
" }\n" +
" }\n" +
" ],\n" +
" \"-110.2324214532214,27.9288762948267\":[\n" +
" {\n" +
" \"data\":{\n" +
" \"date\":\"2017-13-13\",\n" +
" \"source_type\":\"Light\",\n" +
" \"table_value\":3,\n" +
" \"condition\":\"Good\"\n" +
" }\n" +
" },\n" +
" {\n" +
" \"data\":{\n" +
" \"date\":\"2019-11-15\",\n" +
" \"source_type\":\"Light\",\n" +
" \"table_value\":4,\n" +
" \"condition\":\"Very good\"\n" +
" }\n" +
" },\n" +
" {\n" +
" \"data\":{\n" +
" \"date\":\"2019-11-15\",\n" +
" \"source_type\":\"Heavy\",\n" +
" \"table_value\":3,\n" +
" \"condition\":\"Good\"\n" +
" }\n" +
" }\n" +
" ]\n" +
"}";


try {
JSONObject jsonObject=new JSONObject(jresponse);

JSONArray jsonArrayNames=jsonObject.names();


for (int n=0;n<jsonArrayNames.length();n++)
{

//JSONArray jsonArray=jsonObject.getJSONArray("-109.457154947154,26.4155454751248");
Log.d("jsonarray = ",jsonArrayNames.getString(n)+"");
String sjsonarrayname=jsonArrayNames.getString(n)+"";
JSONArray jsonArray=jsonObject.getJSONArray(sjsonarrayname);

if(jsonArray.length()>0)
{
for(int i=0;i<jsonArray.length();i++)
{
JSONObject xobj=jsonArray.getJSONObject(i);
JSONObject data_object=xobj.getJSONObject("data");

String date=data_object.getString("date");
String source_type=data_object.getString("source_type");
String table_value=data_object.getString("table_value");
String condition=data_object.getString("condition");

Toast.makeText(getApplicationContext(),date+"\n"+source_type+"\n"+table_value+"\n"+condition,Toast.LENGTH_LONG).show();

Log.d("date=",date+"");
Log.d("source_type=",source_type+"");
Log.d("table_value=",table_value+"");
Log.d("condition=",condition+"");
Log.d("========","========");

arrayListData.add(new Data(date,source_type,table_value,condition));
}
}

}


}
catch (Exception e)
{
Log.d("error=",e+"");
}


}

关于android - 没有键值的 JSON 序列化响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58214791/

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