jquery如何解析数组(JSONArray)?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了jquery如何解析数组(JSONArray)?相关的知识,希望对你有一定的参考价值。
格式如下:["id":1,"sex":"","age":2,"userName":"1","job":"","password":"","id":2,"sex":"","age":2,"userName":"1","job":"","password":""]jquery如下:$.getJSON( '/MyWeb/pageing/excute-ajax_post.action',
function(data) 这里如何解析?代码如何写?谢谢各位了。 );
语法:
ECMAScript v3规定了数组直接量的语法,javascript 1.2和JScript 3.0实现了它。可以把—个用逗号分隔的表达式列表放在方括号中,创建并初始化—个数组。这些表达式的值将成为数组元素。例如:
var a = [1, true, \'abc\'];
具体操作查看API.
ps:必须方括号隔开。
2.关联数组
1.语法:
var myhash= ”key1″:”val1″, “key2″:”val2″ ;//obj
2.var
myhash= key1:”val1″, key2:”val2″ ;//obj-也可以
ps:跟json格式几乎相同,但是json格式要求更加严格(里面的键值对必须使用双引号),但json只能作为一种格式标准,如果要对其进行操作必须转换成关联数组对象(obj)。
2.简单操作
1.向Hash关联数组添加键值
// 添加一个新键 newkey ,键值为 newval
myhash[”newkey”] = “newval”;
2.删除Hash关联数组已有键值
// 删除一个键 newkey ,同时,该键值对应的 newval 也就消失了
delete myhash[”newkey”];
3.遍历Hash关联数组
// 遍历整个hash 数组
for (key in myhash)
val = myhash[key];
4.获得值
方式1.myhash.key1
方式2.myhash.key2
3.json
格式要求:
”key1″:”val1″, “key2″:”val2″ ;//严格按照此格式,操作可依照关联数组的操作
二.前后台交互中几个关键点
1.当服务器发送的数据不是一条json,而是多条json时,则应当联系数组和关联数组来组装字符串
例如:var objs = [ id: 1, name: \'n_1\' , id: 2, name: \'n_2\'];
2.至始至终服务器给客户端的数据都只是字符串,因此为了让其能够在js中对其进行必要的操作,可以通过eval()进行转换成js可执行的对象。
因此jQuey中提供的$.parseJSON()是有局限的,如果是上面1提到的这种情况则就必须使用eval()进行转换,然后再通过$.each(objs,function(i,o)...)进行操作 参考技术A var arr=eval(data);
for(var i=0;i<arr.length;i++)
alert(arr[i].id);
art(arr[i].sex);
...
参考技术B 试试
data[0].id 参考技术C 试试
data[0].id 参考技术D $.each(data, function(index, obj) //index是索引,obj是第几个元素
alert(val.id);alert(val.userName);
);
无法使用改造解析对象内部 jsonarray 的 jsonarray?
【中文标题】无法使用改造解析对象内部 jsonarray 的 jsonarray?【英文标题】:unable to parse jsonarray of jsonarray inside object using retrofit? 【发布时间】:2020-05-21 00:14:01 【问题描述】:我正在尝试解析对象数据列表中的JSON array
payment_details
。
在这里,我已成功解析在回收器视图中调用的数据数组,但我无法调用 payment_details
数组。
Adapter.class
public class InProgress_Adapter extends RecyclerView.Adapter<InProgress_Adapter.InProgressViewHolder>
private LayoutInflater inflater;
private List<Data_Inprogress> modelRecyclerArrayList;
Context ctx;
public InProgress_Adapter( Context ctx, List<Data_Inprogress> modelRecyclerArrayList)
this.inflater = inflater.from(ctx);
this.modelRecyclerArrayList = modelRecyclerArrayList;
this.ctx = ctx;
@NonNull
@Override
public InProgressViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType)
View view = inflater.inflate(R.layout.in_progress_adapter, viewGroup, false);
InProgressViewHolder holder = new InProgressViewHolder(view);
return holder;
@Override
public void onBindViewHolder(@NonNull InProgressViewHolder holder, int i)
for (i=0;i<modelRecyclerArrayList.size();i++)
holder.location1.setText(modelRecyclerArrayList.get(i).getLocaion());
holder.date1.setText(modelRecyclerArrayList.get(i).getDate());
holder.duration1.setText(modelRecyclerArrayList.get(i).getDuration());
holder.username1.setText(modelRecyclerArrayList.get(i).getUser_name());
holder.workerid1.setText(String.valueOf(modelRecyclerArrayList.get(i).getWork_order_id()));
holder.description1.setText(modelRecyclerArrayList.get(i).getDescription());
holder.useraddress1.setText(modelRecyclerArrayList.get(i).getUser_address());
@Override
public int getItemCount()
return modelRecyclerArrayList.size();
public class InProgressViewHolder extends RecyclerView.ViewHolder
TextView location1,date1,username1,duration1,workerid1,description1,useraddress1
,tvorderid,tvdesc,tvusername,tvdate,tvlocation,tvdur,tvaddre,tvpay,tvamt,tvdt,tvcash,tvbal;
public InProgressViewHolder(@NonNull View itemView)
super(itemView);
Typeface font = Typeface.createFromAsset(ctx.getAssets(),"fonts/Uber Move Text.ttf");
location1=itemView.findViewById(R.id.location1);
date1=itemView.findViewById(R.id.date1);
MainActivity.class
private void fetchInPro()
RetrofitInterface jsonPostService = ServiceGenerator.createService(RetrofitInterface.class, "http://littletreasure.org.in/");
Call<InProgress_Response> call = jsonPostService.postRawJSON1();
call.enqueue(new Callback<InProgress_Response>()
@Override
public void onResponse(Call<InProgress_Response> call, Response<InProgress_Response> response)
if (response.isSuccessful())
proDialog.cancel();
List<Data_Inprogress> datumList1 = response.body().getData();
/*for (int i = 0; i < datumList.size(); i++)
datumList.add(response.body().getData().get(i));
*/
if (getActivity()!=null)
proDialog.cancel();
inProgress_adapter = new InProgress_Adapter(getContext(),datumList1);
recyclerView1.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false));
//recyclerView.setHasFixedSize(true);
//recyclerView.notify();
recyclerView1.setAdapter(inProgress_adapter);
这是我的回应
"status": "Success",
"message": "10 results found.",
"data": [
"work_order_id": 1,
"description": "Desc-1",
"locaion": "Locaion-1",
"date": "0-01-2020",
"duration": "1 days",
"user_name": "User name-1",
"user_address": "User address-1",
"payment_details": [
"amount": 0,
"date": "0-01-2020",
"payment_method": "Cash",
"balance": 0
]
,
"work_order_id": 2,
"description": "Desc-2",
"locaion": "Locaion-2",
"date": "1-01-2020",
"duration": "2 days",
"user_name": "User name-2",
"user_address": "User address-2",
"payment_details": [
"amount": 1,
"date": "1-01-2020",
"payment_method": "Cash",
"balance": 2
]
,
"work_order_id": 3,
"description": "Desc-3",
"locaion": "Locaion-3",
"date": "2-01-2020",
"duration": "3 days",
"user_name": "User name-3",
"user_address": "User address-3",
"payment_details": [
"amount": 2,
"date": "2-01-2020",
"payment_method": "Cash",
"balance": 4
]
,
"work_order_id": 4,
"description": "Desc-4",
"locaion": "Locaion-4",
"date": "3-01-2020",
"duration": "4 days",
"user_name": "User name-4",
"user_address": "User address-4",
"payment_details": [
"amount": 3,
"date": "3-01-2020",
"payment_method": "Cash",
"balance": 6
]
,
"work_order_id": 5,
"description": "Desc-5",
"locaion": "Locaion-5",
"date": "4-01-2020",
"duration": "5 days",
"user_name": "User name-5",
"user_address": "User address-5",
"payment_details": [
"amount": 4,
"date": "4-01-2020",
"payment_method": "Cash",
"balance": 8
]
,
"work_order_id": 6,
"description": "Desc-6",
"locaion": "Locaion-6",
"date": "5-01-2020",
"duration": "6 days",
"user_name": "User name-6",
"user_address": "User address-6",
"payment_details": [
"amount": 5,
"date": "5-01-2020",
"payment_method": "Cash",
"balance": 10
]
,
"work_order_id": 7,
"description": "Desc-7",
"locaion": "Locaion-7",
"date": "6-01-2020",
"duration": "7 days",
"user_name": "User name-7",
"user_address": "User address-7",
"payment_details": [
"amount": 6,
"date": "6-01-2020",
"payment_method": "Cash",
"balance": 12
]
]
这是我的 InProgress_Response
public class InProgress_Response
private String status;
private String message;
/*data list of response*/
private List<Data_Inprogress> data=null;
public String getStatus()
return status;
public void setStatus(String status)
this.status = status;
public String getMessage()
return message;
public void setMessage(String message)
this.message = message;
public List<Data_Inprogress> getData()
return data;
public void setData(List<Data_Inprogress> data)
this.data = data;
这是我的 Data_Inprogress
public class Data_Inprogress
private String description;
private Integer work_order_id;
private String locaion;
private String date;
private String duration;
private String user_name;
private String user_address;
@SerializedName("payment_details")
@Expose
private List<Payment_Inprogress> paymentDetails=null;
public List<Payment_Inprogress> getPaymentDetails()
return paymentDetails;
public void setPaymentDetails(List<Payment_Inprogress> paymentDetails)
this.paymentDetails = paymentDetails;
public String getDescription()
return description;
public void setDescription(String description)
this.description = description;
public int getWork_order_id()
return work_order_id;
public void setWork_order_id(int work_order_id)
this.work_order_id = work_order_id;
public String getLocaion()
return locaion;
public void setLocaion(String locaion)
this.locaion = locaion;
public String getDate()
return date;
public void setDate(String date)
this.date = date;
public String getDuration()
return duration;
public void setDuration(String duration)
this.duration = duration;
public String getUser_name()
return user_name;
public void setUser_name(String user_name)
this.user_name = user_name;
public String getUser_address()
return user_address;
public void setUser_address(String user_address)
this.user_address = user_address;
【问题讨论】:
请出示您的InProgress_Response.class
可以添加InProgress_Response
和Data_Inprogress
型号吗?
请再看一遍上面。我添加了 InProgress_Response 类和 Data_InProgress 类模型。
为什么onBindViewHolder里面有for循环?
@Ashwini Violet 非常感谢您的努力,这很有帮助,而且效果很好。亲爱的,您救了我的命。再次感谢你:)
【参考方案1】:
你可以访问像
这样的值 @Override
public void onBindViewHolder(@NonNull InProgressViewHolder holder, int i)
holder.location1.setText(modelRecyclerArrayList.get(i).getLocaion());
holder.date1.setText(modelRecyclerArrayList.get(i).getDate());
holder.duration1.setText(modelRecyclerArrayList.get(i).getDuration());
holder.username1.setText(modelRecyclerArrayList.get(i).getUser_name());
holder.workerid1.setText(String.valueOf(modelRecyclerArrayList.get(i).getWork_order_id()));
holder.description1.setText(modelRecyclerArrayList.get(i).getDescription());
holder.useraddress1.setText(modelRecyclerArrayList.get(i).getUser_address());
// here how you access the values, here method name is based on convention, change with your get method name if different
holder.tvamt.setText(modelRecyclerArrayList.get(i).getPaymentDetails().get(0).getAmount());
holder.tvdt.setText(modelRecyclerArrayList.get(i).getPaymentDetails().get(0).getDate());
holder.tvcash.setText(modelRecyclerArrayList.get(i).getPaymentDetails().get(0).getPaymentMethod());
holder.tvbal.setText(modelRecyclerArrayList.get(i).getPaymentDetails().get(0).getBalance());
这里假设您的一系列付款详细信息只有一个对象(正如您在回复中向我们展示的那样)
【讨论】:
【参考方案2】:编辑您的 Data_Inprogress 模型
public class Data_Inprogress
private String description;
private Integer work_order_id;
private String locaion;
private String date;
private String duration;
private String user_name;
private String user_address;
private List <Data_Inprogress> payment_details;
private double amount;
private String date;
private String payment_method;
private double balance;
//create setter, getter
将此添加到您的活动方法中
private void fetchInPro()
RetrofitInterface jsonPostService = ServiceGenerator.createService(RetrofitInterface.class, "http://littletreasure.org.in/");
Call<InProgress_Response> call = jsonPostService.postRawJSON1();
call.enqueue(new Callback<InProgress_Response>()
@Override
public void onResponse(Call<InProgress_Response> call, Response<InProgress_Response> response)
if (response.isSuccessful())
proDialog.cancel();
List<Data_Inprogress> datumList1 = response.body();
/*for (int i = 0; i < datumList.size(); i++)
datumList.add(response.body().getData().get(i));
*/
/*----------------add this line ---------------*/
List<Data_Inprogress> paymentDtlLst = datumList1.getPayment_details();
//access nested array
for(Data_Inprogress payDtl : paymentDtlLst)
double amount=payDtl.getAmount();
String date=payDtl.getDate();
String payment_method= payDtl.getPayment_method();
double balance=payDtl.getBalance();
// Log.d("payment_details", data+payment_method);
/*----------------end---------------*/
if (getActivity()!=null)
proDialog.cancel();
inProgress_adapter = new InProgress_Adapter(getContext(),datumList1);
recyclerView1.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false));
//recyclerView.setHasFixedSize(true);
//recyclerView.notify();
recyclerView1.setAdapter(inProgress_adapter);
希望它对你有用。
【讨论】:
我刚刚尝试了您的代码,它的工作原理非常感谢您。 :)以上是关于jquery如何解析数组(JSONArray)?的主要内容,如果未能解决你的问题,请参考以下文章
后台封装成jsonarray,前台js如何接收并存储到下拉列表中,急急急。。。
无法使用改造解析对象内部 jsonarray 的 jsonarray?
使用 SwiftyJSON 将 JSONArray 解析为字符串数组