2013年10月31日星期四

JSON into an array

[{'name':'li','age':'13'},{'name':'yang','age':'20'}...]
, similar to this , I want to get every one of the second attribute value corresponding note : there's a name and age are not fixed, so I'm thinking can turn into an array
[['li','13'],['yang','20']]
, so that you can subscript to the value of ?
------ Solution - -------------------------------------------
java can be directly traverse jsonArray of



String json = command.substring (6);
JSONArray jsonArray = (JSONArray) JSONSerializer.toJSON (json);
String json_c = jsonArray.getString (0);
JSONObject jsonObject = JSONObject.fromObject (json_c);
for (Iterator iter = jsonObject.keys (); iter.hasNext () ;) {/ / first object traversing the entire people
String key = (String) iter.next ();
System.out.println (key + "#" + jsonObject.getString (key));
}
------ Solution ------------------------------- -------------
var obj = [];
var o;
var d;
var t = [{
'name': 'li',
'age': '13'
},
{
'name': 'yang',
'age': '20'
}];
for (var i = 0; i < t.length; i++) {
o = t[i];
d = [];
for (var u in o) {
d.push(o[u]);
}
obj.push(d);
}

------ Solution --------------------- -----------------------

this ?

var t3="[['<a href=# onclick=openLink(14113295100,社旗县国税局桥头税务所,14113295100,d6d223892dc94f5bb501d4408a68333d,swjg_dm);>14113295100</a>','社旗县国税局桥头税务所','社旗县城郊乡长江路西段']]";  
//通过eval() 函数可以将JSON字符串转化为对象  
var obj = eval(t3);  
for(var i=0;i<obj.length;i++){  

alert(obj[i][1]);//输出第二个  

}  

this ?

------ For reference only ---------------------------------- -----

Oh , sorry , add Ha, I this is in JavaScript , and ask how to do it
------ For reference only ----- ----------------------------------

Oh , sorry , add Kazakhstan, I this is in JavaScript , may I ask how do  


jsonArray:
var data = [{name: "a", age: 12}, {name: "b", age: 11}, {name: "c", age: 13}, {name : "d", age: 14}];
for (var o in data) {
alert (o);
alert (data [o]);
alert ("text:" + data [o]. name + "value:" + data [o]. age);
}

If json string:

var t = "{'firstName': 'cyra', 'lastName': 'richardson', 'address': ; {'streetAddress': '1 Microsoft way', 'city': 'Redmond', 'state': 'WA', 'postalCode': 98052}, 'phoneNumbers': ['425-777-7777 ', '206-777-7777']} ";



var jsonobj = eval ('(' + t + ')') ;/ / single JSON object to add braces , JSON array is not required

alert (jsonobj.firstName);

alert (jsonobj.lastName);



------ For reference only ---------------------------------- -----

Oh , sorry , add Ha, I this is in JavaScript , and ask how to do it          
  
  
jsonArray:   
var data = [{name: "a", age: 12}, {name: "b", age: 11}, {name: "c", age: 13}, {name : "d", age: 14}];   
for (var o in data) {   
alert (o);   
alert (data [o]);   
alert ("text:" + data [o]. name + "value:" + data [o]. age);   
}   
  
If json string:   
  
var t = "{'firstName': 'cyra', 'lastName': 'richardson', 'address': ; {'streetAddress': '1 Microsoft way', 'city': 'Redmond', 'state': 'WA', 'postalCode': 98052}, 'phoneNumbers': ['425-777-7777 ', '206-777-7777']} ";   
  
  
  
var jsonobj = eval ('(' + t + ')') ;/ / single JSON object to add braces , JSON array is not required   
  
alert (jsonobj.firstName);   
  
alert (jsonobj.lastName);   
  
  
  Maybe you did not look carefully to my question , because I am the attribute name is not fixed , you can not pass . firstName way to get
------ For reference only ---------------------------------------
I try
- ----- For reference only ---------------------------------------
gray often swim ah !
------ For reference only ---------------------------------------

strange
this.JSON2Array=function(json){
var obj=[];
for(var o in json){
obj.push(json[o]);
}
alert('obj is Array?'+obj instanceof Array);//为什么是false?
return obj;
}

------ For reference only ----------------------------------- ----

strange   
this.JSON2Array=function(json){
var obj=[];
for(var o in json){
obj.push(json[o]);
}
alert('obj is Array?'+obj instanceof Array);//为什么是false?
return obj;
}
  forget parentheses , and laughed
- ----- For reference only ---------------------------------------
you do not know how pass . It is taken by the subscript
var obj = [];
var o;
var d;
var t = [{
'name': 'li',
'age': '13'
},
{
'name': 'yang',
'age': '20'
}];
for (var i = 0; i < t.length; i++) {
o = t[i];
d = [];
for (var u in o) {
d.push(o[u]);
}
obj.push(d);
}
  
alert(obj instanceof Array);  
alert(obj[0])
alert(obj[0][1])
//----
});
</script>

------ For reference only ----------------------------------- ----
json can be said to be object-oriented , and you turn into an array is not more trouble ?

没有评论:

发表评论