導航:首頁 > 編程語言 > 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返回個數組相關的資料

熱點內容
瀟湘書院sp 瀏覽:324
林正英下載 瀏覽:511
帶著智能手機闖盪異世界 小說 瀏覽:901
男同情愛電影 瀏覽:913
vb監控文件夾 瀏覽:850
台灣丈夫電影 瀏覽:660
戴爾筆記本為什麼連不上無線網路 瀏覽:582
台灣的電影電視劇都用什麼網站 瀏覽:238
米思齊編程土壤濕度感測器怎麼用 瀏覽:208
大寸度愛情電影 瀏覽:213
2015年全球大數據總量 瀏覽:63
建設工程人員配置要求在哪個文件 瀏覽:157
泰國大尺度同性 瀏覽:448
手機excel怎麼保存文件怎麼打開 瀏覽:77
主角叫林白 瀏覽:395
胸很大的電影 瀏覽:899
水裡作愛的電影韓國 瀏覽:404
京東金融java薪酬 瀏覽:205
mastercam91安裝教程 瀏覽:119
ie6翻書js 瀏覽:148

友情鏈接