您当前的位置:首页 > 互联网教程

json 怎么解析

发布时间:2025-05-21 05:57:14    发布人:远客网络

json 怎么解析

一、json 怎么解析

一、 JSON(JavaScript Object Notation)一种简单的数据格式,比xml更轻巧。

1、“名称/值”对的集合(A collection of name/value pairs)。不同的语言中,它被理解为对象(object),纪录(record),结构(struct),字典(dictionary),哈希表(hash table),有键列表(keyed list),或者关联数组(associative array)。如:

2、值的有序列表(An ordered list of values)。在大部分语言中,它被理解为数组(array)如:

{“name”:”jackson”,“age”:100},

{“name”:”michael”,”age”:51}

A、服务器端将数据转换成json字符串

首先、服务器端项目要导入json的jar包和json所依赖的jar包至builtPath路径下(这些可以到JSON-lib官网下载:)

然后将数据转为json字符串,核心函数是:

public static String createJsonString(String key, Object value)

JSONObject jsonObject= new JSONObject();

B、客户端将json字符串转换为相应的javaBean

1、客户端获取json字符串(因为android项目中已经集成了json的jar包所以这里无需导入)

public static String getJsonContent(String urlStr)

{//获取HttpURLConnection连接对象

HttpURLConnection httpConn=(HttpURLConnection) url

httpConn.setConnectTimeout(3000);

httpConn.setRequestMethod("GET");

int respCode= httpConn.getResponseCode();

return ConvertStream2Json(httpConn.getInputStream());

catch(MalformedURLException e)

// TODO Auto-generated catch block

// TODO Auto-generated catch block

private static String ConvertStream2Json(InputStream inputStream)

// ByteArrayOutputStream相当于内存输出流

ByteArrayOutputStream out= new ByteArrayOutputStream();

byte[] buffer= new byte[1024];

while((len= inputStream.read(buffer, 0, buffer.length))!=-1)

jsonStr= new String(out.toByteArray());

// TODO Auto-generated catch block

public static Person getPerson(String jsonStr)

JSONObject jsonObj= new JSONObject(jsonStr);

//得到指定json key对象的value对象

JSONObject personObj= jsonObj.getJSONObject("person");

person.setId(personObj.getInt("id"));

person.setName(personObj.getString("name"));

person.setAddress(personObj.getString("address"));

// TODO Auto-generated catch block

public static List<Person> getPersons(String jsonStr)

List<Person> list= new ArrayList<Person>();

jsonObj= new JSONObject(jsonStr);

//得到指定json key对象的value对象

JSONArray personList= jsonObj.getJSONArray("persons");

for(int i= 0; i< personList.length(); i++)

JSONObject jsonItem= personList.getJSONObject(i);

person.setId(jsonItem.getInt("id"));

person.setName(jsonItem.getString("name"));

person.setAddress(jsonItem.getString("address"));

// TODO Auto-generated catch block

二、js解析json文件,获取数据

1、在JavaScript中处理JSON数据时,首先需要将字符串转换为JS对象,特别是当JSON数据以数组形式返回时。例如,如果接收到的字符串是这样的:

2、str='[{"key":"value","keys":[{"key1":"value1","key2":"value2"},{"key1":"value3","key2":"value4"}],"obj":{"id":1,"msg":"success"}}]'

3、为了将这个字符串转换成JS对象,可以使用eval函数,虽然这种方法存在安全风险,但在某些场景下仍然是可行的:

4、接下来,使用jQuery的$.each()方法遍历这个JS对象数组。$.each()方法允许我们对数组中的每个元素执行操作。在这个例子中,我们可以通过传递两个参数给$.each()方法来实现这一点:

5、$.each(str_json, function(a, b){

6、alert(b.id);//弹出要取出的数据

7、上述代码中,a代表数组中的索引,b代表数组中的值。通过这种方式,我们可以逐个获取并处理JSON对象中的数据。需要注意的是,eval函数虽然简单实用,但在处理JSON数据时,推荐使用JSON.parse()方法来转换字符串为JS对象,以提高代码的安全性和可读性。

三、jQuery怎么解析Json字符串

使用这种方法,必须在Ajax请求中设置参数:

获取通过回调函数返回的数据并解析得到我们想要的值,看源码:

});

jquery异步请求将type(一般为这个配置属性)设为“json”,或者利用$.getJSON()方法获得服务器返回,那么就不需要eval()方法了,因为这时候得到的结果已经是json对象了,只需直接调用该对象即可,这里以$.getJSON方法为例:

jquery异步请求将type(一般为这个配置属性)设为“json”,或者利用$.getJSON()方法获得服务器返回,那么就不需要eval()方法了,因为这时候得到的结果已经是json对象了,只需直接调用该对象即可,这里以$.getJSON方法为例:

{name:'1',value:'0'},

{name:'6101',value:'北京市'},

{name:'6102',value:'天津市'},

{name:'6103',value:'上海市'},

{name:'6104',value:'重庆市'},

{name:'6105',value:'渭南市'},

{name:'6106',value:'延安市'},

{name:'6107',value:'汉中市'},

{name:'6108',value:'榆林市'},

{name:'6109',value:'安康市'},

{name:'6110',value:'商洛市'}

$.getJSON("",{param:"sanic"},function(data){

//此处返回的data已经是json对象

$.each(data.root,function(idx,item){

returntrue;//同countinue,返回false同break

alert("name:"+item.name+",value:"+item.value);

});

二、jQuery解析Json对象:

jQuery提供了另一种方法“parseJSON”,这需要一个标准的JSON字符串,并返回生成的JavaScript对象。语法:

看看它是如何运用的到实际开发中的:

url:dataURL,success:function(results){

varparsedJson=jQuery.parseJSON(results);