JSON 序列化没有键值的响应
JSON serialize response without key values
我对 http 调用使用改造,对 json 解析使用 gson。
这是我的 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"
}
}
]
}
此响应没有顶级元素的键。我想序列化这个响应。
这是我的数据object.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 开发的新手,所以我无法理解如何序列化其他元素。我知道我应该编写另一个模型 class 来解析此 JSON 响应。但是我想不出正确的做法。
"-109.4571..."、"-110.23..." 是键,不是值。如果你想获取密钥,你可以看到。
如果要解析对象,应将 json 修复为:
{
"string_key_1":"-109.457154947154,26.4155454751248",
"string_key_2":[
{
"data": {
"date": "2017-13-13",
"source_type": "Light",
"table_value": 3,
"condition": "Good"
}
}
]
}
在您的示例中也从 json 数组的不同键获取此动态 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+"");
}
}
我对 http 调用使用改造,对 json 解析使用 gson。 这是我的 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"
}
}
]
}
此响应没有顶级元素的键。我想序列化这个响应。
这是我的数据object.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 开发的新手,所以我无法理解如何序列化其他元素。我知道我应该编写另一个模型 class 来解析此 JSON 响应。但是我想不出正确的做法。
"-109.4571..."、"-110.23..." 是键,不是值。如果你想获取密钥,你可以看到
如果要解析对象,应将 json 修复为:
{
"string_key_1":"-109.457154947154,26.4155454751248",
"string_key_2":[
{
"data": {
"date": "2017-13-13",
"source_type": "Light",
"table_value": 3,
"condition": "Good"
}
}
]
}
在您的示例中也从 json 数组的不同键获取此动态 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+"");
}
}