json字符串数组解析(java解析json字符串数组)
js怎么解析json成数组?
//通过eval() 函数可以将JSON字符串转化为对象
var obj = eval(t3);
for(var i=0;iobj.length;i++){
for(var j=0;jobj[i].length;j++){
alert(obj[i][j]);}}/*//stringifier 函数的作用跟 parse 相反, 用来将一个js对象转换为 JSON 文本。
var jsonText = JSON.stringify(obj);
alert(jsonText);
//如果基于安全的考虑的话,最好是使用一个 JSON 解析器。 一个 JSON 解析器将只接受 JSON 文本。所以是更安全的。
var myObject = JSON.parse(myJSONtext, filter);
//可选的 filter 参数将遍历每一个value key 值对, 并进行相关的处理。如://如myData = JSON.parse(text, function (key, value) {
return key.indexOf('date') = 0 ? new Date(value) : value; });
json数据中有json数组 怎么解析
实例:
import net.sf.json.JSONArray;
public class TestJson
{
public static void main(String []args)
{
String json = "[{\"a\":\"111\",\"b\":\"222\",\"c\":\"333\"},{\"a\":\"1000\",\"b\":\"2000\",\"c\":\"000\"},{\"a\":\"999\",\"b\":\"300\",\"c\":\"700\"}]";
JSONArray jsonArr = JSONArray.fromObject(json);
String a[] = new String[jsonArr.size()];
String b[] = new String[jsonArr.size()];
String c[] = new String[jsonArr.size()];
for (int i = 0; i jsonArr.size(); i++) {
a[i] = jsonArr.getJSONObject(i).getString("a");
b[i] = jsonArr.getJSONObject(i).getString("b");
c[i] = jsonArr.getJSONObject(i).getString("c");
}
for (int i = 0; i c.length; i++) {
System.out.print(a[i]+" ");
System.out.print(b[i]+" ");
System.out.print(c[i]);
System.out.println();
}
}
}
JSONArray的解析
问题1:类似String s = "[ { },{ },{ } ]";这种结构的解析
方法一:
Gson gson =newGson();
ListObject? mList = gson.fromJson(result, newTypeTokenListObject(){}.getType());
类似下图:
方法二:
1.将该String字符串转为JSONArray
JSONArray objects =JSONArray.parseArray(result);
2.遍历这个JSONArray,将每个JSONArray转为对应的实体
for(intj=0;jobjects.size();j++){
JSONObject jsonObject = objects.getJSONObject(j);
Object mObject= JSONObject.parseObject(jsonObject.toString(),Object.class);
mList.add(mObject); ? ?//添加到与之对应的list集合
}
类似下图
急,JS这种数组JSON字符串怎么解析
var?data?=?[
????[
????????{
????????????"lng":?116.306138,
????????????"lat":?39.844902
????????},
????????{
????????????"lng":?116.306138,
????????????"lat":?39.844902
????????},
????????{
????????????"lng":?116.271643,
????????????"lat":?39.756208
????????},
????????{
????????????"lng":?116.271643,
????????????"lat":?39.756208
????????},
????????{
????????????"lng":?116.491261,
????????????"lat":?39.818306
????????},
????????{
????????????"lng":?116.491261,
????????????"lat":?39.818306
????????}
????],
????[
????????{
????????????"lng":?116.424571,
????????????"lat":?39.731353
????????},
????????{
????????????"lng":?116.424571,
????????????"lat":?39.731353
????????},
????????{
????????????"lng":?116.387776,
????????????"lat":?39.68517
????????},
????????{
????????????"lng":?116.518857,
????????????"lat":?39.702936
????????}
????]
]
data[0][0].lng
如何通过gson解析存储的json数组字符串
引入gson-1.4.jar,(挺蛋疼的,不想自己研究这玩意,寻思网上直接找现成的Demo,结果没有我想要的东西)
Gson中重要的几个核心类:
Gson、JsonParser、JsonObject、JsonArray。
下面演示这两个类之间的转换过程:
import Java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
public class GsonTest {
public static void main(String[] args) {
Map map = new HashMap();
map.put("name", "nam1");
map.put("age", "12");
List list = new ArrayList();
list.add("name");
list.add("123");
list.add(map);
//json格式的字符串
String str = "[{'name':'kevin','age':25},{'name':'cissy','age':24}]";
//创建一个Gson对象
Gson gson = new Gson();
//把java对象转换成Json字符串。
String jsonStr = gson.toJson(map);
System.out.println(jsonStr);
//把集合对象转换成Json字符串
jsonStr = gson.toJson(list);
System.out.println(jsonStr);
//创建一个JsonParser
JsonParser parser = new JsonParser();
//通过JsonParser对象可以把json格式的字符串解析成一个JsonElement对象
JsonElement el = parser.parse(str);
//把JsonElement对象转换成JsonObject
JsonObject jsonObj = null;
if(el.isJsonObject()){
jsonObj = el.getAsJsonObject();
}
//把JsonElement对象转换成JsonArray
JsonArray jsonArray = null;
if(el.isJsonArray()){
jsonArray = el.getAsJsonArray();
}
//遍历JsonArray对象
MyField field = null;
Iterator it = jsonArray.iterator();
while(it.hasNext()){
JsonElement e = (JsonElement)it.next();
//JsonElement转换为JavaBean对象
field = gson.fromJson(e, MyField.class);
System.out.println(field.getName()+" === "+field.getAge());
}
}
}
class MyField {
private String name;
private Integer age;
public MyField() {
}
public MyField(String name, Integer age) {
super();
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
使用gson解析怎么将json字符串解析为数组
步骤1:目标:将从webservice传回的json
1
{
"status": 0,
"result": {
"location": {
"lng": 103.98964143811,
"lat": 30.586643130352
},
"formatted_address": "四川省成都市双流县北一街154",
"business": "簇桥,金花桥",
"addressComponent": {
"city": "成都市",
"district": "双流县",
"province": "四川省",
"street": "北一街",
"street_number": "154"
},
"cityCode": 75
}
}
2
先普及下json数据格式定义: json数据只有两种格式.
一种是对象: 一个大括号包裹的内容就是一个对象.里面是无数个逗号相间隔的键值对
{ "firstName": "Brett", "lastName":"McLaughlin", "email": "aaaa" }
一种是数组:一个方括号包裹的内容就是一个数组,里面是无数个逗号相间隔的json对象
如:
{
"people": [
{
"firstName": "Brett",
"lastName": "McLaughlin",
"email": "aaaa"
},
{
"firstName": "Jason",
"lastName": "Hunter",
"email": "bbbb"
},
{
"firstName": "Elliotte",
"lastName": "Harold",
"email": "cccc"
}
]
}
END
步骤2 定义json数据格式对应的javaBean
1
public class Result {
private Integer status;
private ResultDetail result;
public Result() {
}
public Result(Integer status, ResultDetail result) {
super();
this.status = status;
this.result = result;
}
public ResultDetail getResult() {
return this.result;
}
public Integer getStatus() {
return this.status;
}
public void setResult( ResultDetail result ) {
this.result = result;
}
public void setStatus( Integer status ) {
this.status = status;
}
@Override
public String toString() {
return "Result [status=" + this.status + ", result=" + this.result
+ "]";
}
}
2
public class ResultDetail {
Location location;
String formatted_address;
AddressComponent addressComponent;
String business;
String cityCode;
public ResultDetail() {
super();
// TODO Auto-generated constructor stub
}
public ResultDetail(Location location, String formatted_address,
AddressComponent addressComponent, String business, String cityCode) {
super();
this.location = location;
this.formatted_address = formatted_address;
this.addressComponent = addressComponent;
this.business = business;
this.cityCode = cityCode;
}
public AddressComponent getAddressComponent() {
return this.addressComponent;
}
public String getBusiness() {
return this.business;
}
public String getCityCode() {
return this.cityCode;
}
public String getFormatted_address() {
return this.formatted_address;
}
public Location getLocation() {
return this.location;
}
public void setAddressComponent( AddressComponent addressComponent ) {
this.addressComponent = addressComponent;
}
public void setBusiness( String business ) {
this.business = business;
}
public void setCityCode( String cityCode ) {
this.cityCode = cityCode;
}
public void setFormatted_address( String formatted_address ) {
this.formatted_address = formatted_address;
}
public void setLocation( Location location ) {
this.location = location;
}
}
3
public class Location {
String lng;
String lat;
public Location() {
}
public Location(String lng, String lat) {
this.lng = lng;
this.lat = lat;
}
public String getLat() {
return this.lat;
}
public String getLng() {
return this.lng;
}
public void setLat( String lat ) {
this.lat = lat;
}
public void setLng( String lng ) {
this.lng = lng;
}
@Override
public String toString() {
return "Location [lng=" + this.lng + ", lat=" + this.lat + "]";
}
}
4
public class AddressComponent {
String city;
String district;
String province;
String street;
String street_number;
public AddressComponent() {
super();
// TODO Auto-generated constructor stub
}
public AddressComponent(String city, String district, String province,
String street, String street_number) {
super();
this.city = city;
this.district = district;
this.province = province;
this.street = street;
this.street_number = street_number;
}
public String getCity() {
return this.city;
}
public String getDistrict() {
return this.district;
}
public String getProvince() {
return this.province;
}
public String getStreet() {
return this.street;
}
public String getStreet_number() {
return this.street_number;
}
public void setCity( String city ) {
this.city = city;
}
public void setDistrict( String district ) {
this.district = district;
}
public void setProvince( String province ) {
this.province = province;
}
public void setStreet( String street ) {
this.street = street;
}
public void setStreet_number( String street_number ) {
this.street_number = street_number;
}
@Override
public String toString() {
return "AddressComponent [city=" + this.city + ", district="
+ this.district + ", province=" + this.province + ", street="
+ this.street + ", street_number=" + this.street_number + "]";
}
}
5
测试:
jsonString ( 目标json数据,已经在最上面写好的)
System.out.println( "jsonString:" + jsonString );
Gson gson = new Gson();
Result fromJson = gson.fromJson( jsonString.toString() ,
Result.class );
System.out.println( "******************************************" );
System.out.println( fromJson );
6
结果:
jsonString:{"status":0,"result":{"location":{"lng":103.98964143811,"lat":30.586643130352},"formatted_address":"四川省成都市双流县北一街154","business":"簇桥,金花桥","addressComponent":{"city":"成都市","district":"双流县","province":"四川省","street":"北一街","street_number":"154"},"cityCode":75}}
*******************************************
Result [status=0, result=ResultDetail [location=Location [lng=103.98964143811, lat=30.586643130352], formatted_address=四川省成都市双流县北一街154, addressComponent=AddressComponent [city=成都市, district=双流县, province=四川省, street=北一街, street_number=154], business=簇桥,金花桥, cityCode=75]]
7
可见,jsonString已经成功的被转换成了对应的javaBean
END
步骤3 : 总结.说明
Gson可以很轻松的实现javaBean和jsonString之间的互转.只需要明白json如何定义.剩下的就非常简单了.
推荐使用该框架,在网上看到过一篇解析json的各种方式的效率比较. Gsoon的效率是最高的.
再推荐一个jsonString格式化工具.那样能方便你的阅读-地址可以在参考资料中找到
4
当然上面的例子只是Gson的很小的一个用例,对于将javabean转换成jsonString,以及更为复杂的使用.请关注我后续经验