json对象的值为数组,怎么获取数组某项的值?(注意不是json数组) 如何获取json的数组数值
\u5982\u4f55\u83b7\u53d6jsonarray\u4e2d \u67d0\u4e2a\u503c1\u3001\u521b\u5efa\u4e00\u4e2ajsonArray\u5bf9\u8c61\u3002\u91cc\u9762\u5305\u542b\u91cd\u590d\u7684\u5185\u5bb9\uff0c\u5982\u56fe\u3002
2\u3001\u7136\u540e\u521b\u5efa\u4e00\u4e2aset\u96c6\u5408\uff0c\u56e0\u4e3aset\u96c6\u5408\u4e2d\u7684\u5143\u7d20\u4e0d\u91cd\u590d\u5177\u6709\u53bb\u91cd\u7684\u6548\u679c\u3002
3\u3001\u901a\u8fc7\u5faa\u73af\u628ajsonArray\u5bf9\u8c61\u4e2d\u7684\u5143\u7d20\u90fd\u6dfb\u52a0\u5230set\u96c6\u5408\u4e14\u5148\u8f93\u51fa\u5143\u5bf9\u8c61\u7684\u5185\u5bb9\u6765\u770b\u6548\u679c\u3002
4\u3001\u628ajsonArray\u6dfb\u52a0\u5230set\u96c6\u5408\u540e\uff0c\u4ece\u4e0a\u4e00\u6b65\u53ef\u4ee5\u770b\u51fa\u662f3\u6761\u8bb0\u5f55\uff0c\u8fd9\u91cc\u628aset\u96c6\u5408\u7684\u957f\u5ea6\u8f93\u51fa\u770b\u4e0b\u5b83\u7684\u5927\u5c0f\u53ef\u4ee5\u53d1\u73b0\u53d8\u5c0f\u4e86\u3002
5\u3001\u518d\u628aset\u96c6\u5408\u8f6c\u6210\u5b57\u7b26\u4e32\u518d\u8f6c\u6362\u6210jsonArray\u5bf9\u8c61\u3002
6\u3001\u91cd\u65b0\u4f7f\u7528for\u5faa\u73af\u628a\u65b0\u8d4b\u503c\u7684jsonArray\u5bf9\u8c61\u503c\u8f93\u51fa\u5c31\u5b8c\u6210\u4e86\u3002
\u5728JS\u4e2d:
\u5047\u8bbe\u4f60\u7684json \u5bf9\u8c61\u662f jObject
\u83b7\u53d6\u6570\u7ec4
var picArr = jObject['taobao_pic_urls'];
\u83b7\u53d6\u5176\u4e2d\u7684\u67d0\u9879\u503c
var index = 0; //\u8981\u83b7\u53d6\u7684\u4e0b\u6807
var picUrl = picArr[index];
\u5728java\u4e2d:
\u53ef\u4ee5\u4f7f\u7528net.sf.JsonObject \u548c net.sf.JsonArray
\u5047\u8bbe\u4f60\u7684json \u5bf9\u8c61\u662f jObject net.sf.JsonObject\u5bf9\u8c61\u683c\u5f0f
\u83b7\u53d6\u6570\u7ec4
JosnArray jArray = jObject.getJSONArray("taobao_pic_urls");
\u83b7\u53d6\u5176\u4e2d\u7684\u67d0\u9879\u503c
int index = 0; //\u8981\u83b7\u53d6\u7684\u4e0b\u6807
String picUrl = String.valueOf(jArray.get(index));
package com;
import java.util.List;
import com.Kensou.Person;
import com.alibaba.fastjson.JSON;
public class Yuma
{
public static void main(String[] args)
{
String result =
"{\"data1\":[{\"name\":\"aa\",\"age\":\"12\"},{\"name\":\"bb\",\"age\":\"13\"}],\"data2\":{\"nowpage\":1,\"pagesize\":2}}";
Kensou ps = JSON.parseObject(result, Kensou.class);
System.out.println(ps);
List<Person> data1 = ps.getData1();
for(int i = 0; i < data1.size(); i++)
{
Person p = data1.get(i);
System.out.println(p);
}
Person data2 = ps.getData2();
System.out.println(data2);
}
}
/**
* 模型对象不需要构造函数, 至少只需要setter方法<br>
* Person类可以是内嵌类,也可以是外部类 <br>
* Kensou类只能是外部类<br>
* 集合类可以是数组或者List类型<br>
* <p>
* 使用的是<font color="red">fastjson-1.1.36.jar</font>
*/
class Kensou
{
private List<Person> data1;
private Person data2;
public List<Person> getData1()
{
return data1;
}
public void setData1(List<Person> data1)
{
this.data1 = data1;
}
public Person getData2()
{
return data2;
}
public void setData2(Person data2)
{
this.data2 = data2;
}
@Override
public String toString()
{
return String.format("Kensou [data1=%s, data2=%s]", data1, data2);
}
class Person
{
private String name;
private int age;
private int nowpage;
private int pagesize;
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public int getAge()
{
return age;
}
public void setAge(int age)
{
this.age = age;
}
public int getNowpage()
{
return nowpage;
}
public void setNowpage(int nowpage)
{
this.nowpage = nowpage;
}
public int getPagesize()
{
return pagesize;
}
public void setPagesize(int pagesize)
{
this.pagesize = pagesize;
}
@Override
public String toString()
{
return String.format("Person [name=%s, age=%s, nowpage=%s, pagesize=%s]", name, age, nowpage, pagesize);
}
}
}
在JS中:
假设你的json 对象是 jObject
获取数组
var picArr = jObject['taobao_pic_urls'];
获取其中的某项值
var index = 0; //要获取的下标
var picUrl = picArr[index];
在java中:
可以使用net.sf.JsonObject 和 net.sf.JsonArray
假设你的json 对象是 jObject net.sf.JsonObject对象格式
获取数组
JosnArray jArray = jObject.getJSONArray("taobao_pic_urls");
获取其中的某项值
int index = 0; //要获取的下标
String picUrl = String.valueOf(jArray.get(index));
json对象的话应该直接可以取出来,你如果通过json方式取不出来很有可能是你的只是个json字符串通过eval("("+data+")");方式将json字符串转换为json对象就可以了
转化成对象
绛旓細鍙互浣跨敤net.sf.JsonObject 鍜 net.sf.JsonArray 鍋囪浣犵殑json 瀵硅薄鏄 jObject net.sf.JsonObject瀵硅薄鏍煎紡 鑾峰彇鏁扮粍 JosnArray jArray = jObject.getJSONArray("taobao_pic_urls");鑾峰彇鍏朵腑鐨勬煇椤瑰 int index = 0; //瑕佽幏鍙栫殑涓嬫爣 String picUrl = String.valueOf(jArray.get(index));
绛旓細1銆佸厛瀹氫箟涓涓猨son鍙橀噺銆2銆佺劧鍚庡畾涔夎繖涓彉閲忕殑鍊硷紝鍏跺疄涓涓猨son瀵硅薄灏辨槸涓涓猳bj瀵硅薄銆3銆佽娣诲姞json瀵硅薄鐨勫厓绱狅紝鐩存帴浣跨敤鏁扮粍涓嬫爣鏂瑰紡锛屾坊鍔犻敭鍜屽煎氨琛屼簡銆4銆佽涓簀son瀵硅薄娣诲姞瀵硅薄锛屾垜浠彲浠ュ厛鍒涘缓涓涓璞°5銆佺劧鍚庝篃鏄娇鐢ㄦ暟缁勪笅鏍囨柟寮忥紝涓哄叾璧嬪间负鏂扮殑瀵硅薄鍙橀噺灏辫浜嗐6銆佸湪娴忚鍣ㄩ噷锛屾垜浠氨鍙互...
绛旓細JSON.stringify(date) //'data'娉細JSON.stringify() 鏂规硶鏄皢涓涓狫avaScript鍊(瀵硅薄鎴栬鏁扮粍)杞崲涓轰竴涓 JSON瀛楃涓层
绛旓細package com;import java.util.List;import com.Kensou.Person;import com.alibaba.fastjson.JSON;public class Yuma{public static void main(String[] args){String result = "{\"data1\":[{\"name\":\"aa\",\"age\":\"12\"},{\"name\":\"bb\",\"age\":\"13\"}],\"data2...
绛旓細var data = { "card": "2", "numbers": { "array1": [ 1, 11, 21, 1211, 111221, 312211 ] }}//鍙栧嚭array1涓殑1鍊硷紝灏卞彲浠ヨ繖鏍凤紝data.mumbers.array1[0];涓婇潰鏄鏍规嵁涓嬫爣鍙栧笺傝繕鍙互寰幆杈撳嚭鎴戠敤Jquery鐨別ach$.each(data.mumbers.array1,function(...
绛旓細json鏁版嵁mobile閲岄潰鍖呭惈浜唗empdata 閭d箞浣犵殑mobile瀵硅薄 閲岄潰涔熼渶瑕佸寘鍚竴涓猼empdata鐨刲ist瀵硅薄鏉ユ帴鏀 class Mobile{ List<Tempdata> tempdata; }class Tempdata{ private float t; ...}
绛旓細缁鐨刣ate鏄涓json瀛楃涓诧紝鍙互鍏堣浆鎴js瀵硅薄锛歭et date_obj=JSON.parse(date);姝ゅ悗date_obj灏辨槸涓涓瀵硅薄锛鍙栧埌杩欎釜鏁扮粍锛歭et arr=date_obj.numbers;鐒跺悗鐢ㄧ储寮曡闂產rr灏辫兘鍙栧埌浠绘剰涓涓厓绱犮傜患鍚涓 JSON.parse(date).numbers[x],x涓烘兂鍙栫殑鍏冪礌绱㈠紩锛屼粠0寮濮 ...
绛旓細for (int i = 0; i < array.length(); i++) {//寰幆json鏁扮粍 JSONObject ob = (JSONObject) array.get(i);//寰楀埌json瀵硅薄 String name= ob.getString("name");//name杩欓噷鏄垪鍚嶇О锛岃幏鍙杍son瀵硅薄涓垪鍚嶄负name鐨勫 System.out.print(name);//杈撳嚭name } catch (JSONException e) {...
绛旓細1銆佸湪MYSQL涓柊寤轰竴涓猼est鏁版嵁搴擄紝鍦ㄩ噷闈㈡柊寤轰竴寮爐ype鏁版嵁琛紝涓嬮潰灏嗕粠璇ヨ〃鑾峰緱鏁版嵁銆2銆佹柊寤轰竴涓猵roject灏忕▼搴忛」鐩3銆佸湪project椤圭洰鍐咃紝鏂板缓涓涓猧ndex妯″潡鏉ヤ粠鏁版嵁搴撹幏寰楁暟鎹紝骞跺皢鏁版嵁杈撳嚭鍦╥ndex.wxml椤甸潰銆4銆佸湪index妯″潡涓璱ndex.js椤甸潰锛屽湪onLoad涓娇鐢╳x.request鏂规硶鍚戞湇鍔$鏂囦欢璇锋眰鏁版嵁銆倁rl涓...
绛旓細浣跨敤for璇彞瀵jsonArray閬嶅巻 var jsonArray = [{"name":"瀹2鐡","num":"1","price":"122"}];// 閬嶅巻json鏁扮粍for (var obj in jsonArray ){ // 灏json瀵硅薄杞崲涓哄瓧绗︿覆 var str = JSON.stringify(jsonArray[obj]); // 灏唈son瀛楃涓茶浆鎹负map var map = eval("("+str+")"...