导航:首页 > 编程语言 > json返回个数组

json返回个数组

发布时间:2021-02-27 15:13:29

js怎么把返回的json数据转成数组

这要看你得到的数据是什么格式的
下面以数组格式举例
得到的json:'[1,2,3,4]'
使用eval()将字符串转为代码
即可得到数组[1,2,3,4]

㈡ jquery 和json返回的类型如何设置数组

echojson_encode(array_values($rows));

或者:

$rows=array();
array_push($rows,array('value'=>'a','num'=>5));
array_push($rows,array('value'=>'b','num'=>1));
echojson_encode($rows);

㈢ jQuery怎么解析ajax返回的json数据,里面包含一个数组

varjson='{"err_code":0,"err_msg":"操作成功!","return_data":[{"usergameid":"10048","agentid":"10057","score":"1","date":"2017/9/615:06:31"},{"usergameid":"10048","agentid":"10057","score":"1","date":"2017/9/617:10:27"},{"usergameid":"10048","agentid":"10057","score":"1","date":"2017/9/622:25:24"}],"num":26}';
json=$.parseJSON(json);
if(!json.err_code){
$('body').append('<pstyle="color:blue;">'+json.err_msg+'</p><br/><p>数据如下:</p><br/><tableid="data-table"><thead><tr><td>agentid</td><td>date</td><td>score</td><td>usergameid</td></tr></thead><tbody></tbody></table>');
vardata=json.return_data;
if(data){
if($.isArray(data)){
for(variindata){
$('#data-table').children('tbody').append('<tr><td>'+data[i].agentid+'</td><td>'+data[i].date+'</td><td>'+data[i].score+'</td><td>'+data[i].usergameid+'</td></tr>');
}
}
}
}

㈣ jQuery处理json数据返回数组和输出的方法

这篇文章主要介绍了jQuery处理json数据返回数组和输出的方法,涉及jQuery操作数组及json的技巧,需要的朋友可以参考下

本文实例讲述了jQuery处理json数据返回数组和输出的方法。分享给大家供大家参考。具体实现方法如下:

代码如下:
/*print
the
json
object
*
*$("selector").print_r_json(json,opts)
:
return
formatted
string
(and
print)
*sprint_r_json
:
just
return
the
string;
*print_r_json
:
return
the
formatted
string
and
print
json
data
*contribute
明河
*
*auth
iorichina
*
*example:
*3
ways
to
use
it
*<script
language="javascript">
*$("selector").print_r_json({"a":"aa","d":{"ef":{"a":"d","d":["a","b"]},"ed":"dd"},"g":"g"},{if_print:true,return_array:true});
*document.write($.sprint_r_json({"a":"aa","d":{"ef":{"a":"d","d":["a","b"]},"ed":"dd"},"g":"g"}));
*$.print_r_json({"a":"aa","d":{"ef":{"a":"d","d":["a","b"]},"ed":"dd"},"g":"g"});
*</script>
*
*/
$.fn.print_r_json
=
function(json,options){
if(typeof(json)!="object")
return
false;
var
opts
=
$.extend({},$.fn.print_r_json.defaults,options);
var
data
=
'';
if(opts.if_print)
{
data
=
$.sprint_r_json(json)
$(this).html('<div
style="font-weight:bold">'+(opts.return_array?'Array':'JSON-DATA')+'</div>'+data);
}
if(opts.array)
{
return
$.json_to_array(json);
}
return
data;
};
$.fn.print_r_json.defaults
=
{
if_print
:
false,//if
print
or
just
return
formatted
string
return_array
:
true
//return
an
Array
};
$.extend({
print_r_json
:
function(json)
{
if(typeof(json)=="object")
{
var
text='<div
style="font-weight:bold;">{</div><div
style="margin-left:25px;">';
document.write('<div
style="font-weight:bold;">{</div><div
style="margin-left:25px;">');
for(var
p
in
json)
{
if(typeof(json[p])=="object")
{
document.write('<div>["'+p+'"]
=>
');
text+='<div>["'+p+'"]
=>
'+$.print_r_json(json[p])+'</div>';
document.write('</div>');
}
else
{
text+='<div>['+((/^d+$/).test(p)?p:('"'+p+'"'))+']
=>
"'+json[p]+'"</div>';
document.write('<div>['+p+']
=>
'+json[p]+'</div>');
}
}
text+='</div><div
style="font-weight:bold;">}</div>';
document.write('</div><div
style="font-weight:bold;">}</div>');
return
(text);
}
else
{
document.write(json);
return
(json);
}
},
sprint_r_json
:
function(json)
{
if(typeof(json)=="object")
{
var
text
=
'<div
style="font-weight:bold;">{</div><div
style="margin-left:25px;">';
for(var
p
in
json)
{
if(typeof(json[p])=="object")
{
text
+=
'<div>["'+p+'"]
=>
'+$.sprint_r_json(json[p])+'</div>';
}
else
{
text
+=
'<div>['+((/^d+$/).test(p)?p:('"'+p+'"'))+']
=>
"'+json[p]+'"</div>';
}
}
text
+=
'</div><div
style="font-weight:bold;">}</div>';
return
(text);
}
else
{
return
(json);
}
},
json_to_array
:
function(json)
{
if(typeof(json)=="object")
{
var
text
=
new
Array();
for(var
p
in
json)
{
if(typeof(json[p])=="object")
{
text[p]
=
$.json_to_array(json[p]);
}
else
{
text[p]
=
json[p];
}
}
return
(text);
}
else
{
return
(json);
}
}
});

希望本文所述对大家的jQuery程序设计有所帮助。

㈤ Servlet怎么返回JSON值到前台,返回的是一个数组

1、实体类

[java] view plain
import java.util.ArrayList;

public class ObjectType {

private String type;
private ArrayList<SubObjectType> subObjects;
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public ArrayList<SubObjectType> getSubObjects() {
return subObjects;
}
public void setSubObjects(ArrayList<SubObjectType> subObjects) {
this.subObjects = subObjects;
}
}

[java] view plain
public class SubObjectType {

private String subtype;
private double fileCount;
private double bytes;
private String timeRange;
public String getSubtype() {
return subtype;
}
public void setSubtype(String subtype) {
this.subtype = subtype;
}
public double getFileCount() {
return fileCount;
}
public void setFileCount(double fileCount) {
this.fileCount = fileCount;
}
public double getBytes() {
return bytes;
}
public void setBytes(double bytes) {
this.bytes = bytes;
}
public String getTimeRange() {
return timeRange;
}
public void setTimeRange(String timeRange) {
this.timeRange = timeRange;
}

}

2、servlet:得到一个对象列表ArrayList<T>,将其转化为jsonArray

[java] view plain
AccountInfoDao = new AccountInfoDao();
ArrayList<ObjectType> objectTypes = new ArrayList<ObjectType>();
objectTypes = .load();

JSONObject jsonObject = new JSONObject();
jsonObject.put("categorys", objectTypes);

JSONArray jsonArray = new JSONArray();
jsonArray.add(jsonObject);
System.out.println(jsonArray);
PrintWriter out = response.getWriter();
out.write(jsonArray.toString());

3、js处理:

[javascript] view plain
function load(){
$.ajax({
type:"post",//请求方式
url:"servlet/AccountInfo",//发送请求地址
dataType:"json",
data:{//发送给数据库的数据
},
//请求成功后的回调函数有两个参数
success:function(data,textStatus){
var objs=eval(data); //解析json对象
var obj = objs[0];

var table = $("#table");
table.empty();
table.append('<tr><th></th><th>类别</th><th>文件个数</th><th>文件大小</th><th>时间范围</th></tr>');

if(obj==null || obj==""){
table.append('<tr><td colspan="5" style="text-align: center; color:red">暂无数据!</td></tr>')
//$("#page").hide();
return false;
}

var categorys = obj.categorys;
for(var i=0;i<categorys.length;i++){
var type = categorys[i].type;
var subObjects = categorys[i].subObjects;
var len = subObjects.length;
table.append('<tr><td rowspan="'+len+'">'+type+'</td>'+'<td>'+subObjects[0].subtype+'</td>'+'<td>'+subObjects[0].fileCount+'</td>'+'<td>'+subObjects[0].bytes+'</td>'+'<td>'+subObjects[0].timeRange+'</td></tr>')
//table.append('<td>'+subObjects[0].subtype+'</td>'+'<td>'+subObjects[0].fileCount+'</td>'+'<td>'+subObjects[0].bytes+'</td>'+'<td>'+subObjects[0].timeRange+'</td></tr>');
for(var j=1;j<len;j++){
table.append('<tr><td>'+subObjects[j].subtype+'</td>'+'<td>'+subObjects[j].fileCount+'</td>'+'<td>'+subObjects[j].bytes+'</td>'+'<td>'+subObjects[j].timeRange+'</td></tr>');
}
}

//为鼠标移动添加样式,鼠标所在行变色,鼠标离开行恢复原状
$("tr:even").addClass("even");
$("th").first().css("text-align","left");
$("th").first().css("padding-left","5px");
$("tr").mouseenter(function(){
$(this).addClass('bs');
});
$("tr").mouseleave(function(){
$(this).removeClass('bs');
});
}
});
}

4、jsp页面

[html] view plain
<table width="100%" border="0" cellspacing="0" cellpadding="0" class="tab" id="table">
<tr>
<th></th>
<th>类别</th>
<th>文件大小</th>
<th>文件个数</th>
<th>时间范围</th>
</tr>
</table>
望采纳,谢谢。

㈥ java jsonObj返回数组问题

JSONObject jsonObject = JSONObject.fromObject(buffer.toString()); Object obj=jsonObject.get('你的对象名称')

接下来就是你的逻辑

㈦ json返回值里,数组和字典的区别

㈧ PHP 如何处理一串JSON 返回给我一个ID数组呢

你得先用json_decode把json字符串转换成数组,然后再遍历这个结果数组,再把里面的ID取出来放到新数组里面,思路是这样,希望对你有帮助

㈨ 在jsp页面用json返回一个数组怎么做

jsp页面,嵌入查询数据库,有点原始了 你可以把数据库查询封装到一个bean里,然后提版供一个查询并返权回json的方法 在你的ajax页面(这个页面是一个jsp文件),Import这个bean,然后实例化,并调用哪个方法即可

㈩ c#如何获得返回json数组中的数组

推荐使用Newtonsoft.Json,这个可以从nuget获取

有三个方案:

  1. 你将json结果复制,然后编辑→选择性粘贴→将JSON粘贴为类

然后使用

varresult=JsonConvert.DeserializeObject<Jobject>(str);//jobject是你粘贴后生成的类型

把结果反序列为对象

varworlds=new{words=""};
varworlds_result=Enumerable.Repeat(worlds,1).ToArray();
varvalue=new{log_id=123,words_result_num=2,worlds_result};
varstr=string.Empty;

varresult=JsonConvert.DeserializeAnonymousType(str,value);

3.使用Newtonsoft.Json将json字符串转化为JObject手动解析获得想要的数据

阅读全文

与json返回个数组相关的资料

热点内容
玛卡编程怎么样 浏览:302
怎么编程伺服器编码 浏览:109
什么是机密文件 浏览:258
网站收录量低应该如何解决 浏览:978
飞跃贷app官网 浏览:337
js正则表达式全为整数 浏览:561
在哪里免费下载大数据 浏览:218
linux怎么做视频网站 浏览:949
安卓舰娘登入不进去 浏览:145
ak47龙鳞升级成什么 浏览:256
联通sim卡怎么升级4g 浏览:120
linux大日志文件 浏览:974
u盘照片文件错误 浏览:493
大铁机车车载行车数据有哪些 浏览:440
app网址格式http怎么写 浏览:980
大数据古代的 浏览:199
编程中巡线模式是什么 浏览:991
武汉职业技术学院网络技术 浏览:97
jscompress 浏览:952
怎样清理win10隐形垃圾文件 浏览:973

友情链接