zoukankan      html  css  js  c++  java
  • json 解析代码 全语言

    Javascript:

    
    1.使用eval
    var parse_json_by_eval = function(str){
        return eval('('+str+')');
    }
    var value = 1;
    var jsonstr = '{"name":"jifeng","company":"taobao","value":++value}';
    var json1 = parse_json_by_eval(jsonstr);
    console.log(json1);
    console.log('value: '+ value);
    執行結果:
    { name: 'jifeng', company: 'taobao', value: 2 }
    value: 2
    2.使用JSON.parse
    var parse_json_by_JSON_parse = function(str){
        return JSON.parse(str);
    }
    value = 1;
    var jsonstr = '{"name":"jifeng","company":"taobao"}';
    var json2 = parse_json_by_JSON_parse(jsonstr);
    console.log(json2);
    console.log(value);
    From:http://www.cnblogs.com/lengyuhong/archive/2012/01/07/2262390.html
    

    PHP:

    
    $json_string='{"id":1,"name":"jb51","email":"admin@jb51.net","interest":["wordpress","php"]} ';
    $obj=json_decode($json_string);
    echo $obj->name; //prints foo
    echo $obj->interest[1]; //prints php 

    Java:

    
    JSONObject  dataJson=new JSONObject("你的Json数据“);
    JSONObject  response=dataJson.getJSONObject("response");
    JSONArray data=response.getJSONArray("data");
    JSONObject info=data.getJSONObject(0);
    String province=info.getString("province");
    String city=info.getString("city");
    String district=info.getString("district");
    String address=info.getString("address");
     System.out.println(province+city+district+address);

    C#:

    
    使用开源的类库Newtonsoft.Json(下载地址http://json.codeplex.com/)。下载后加入工程就能用。通常可以使用JObject, JsonReader, JsonWriter处理。这种方式最通用,也最灵活,可以随时修改不爽的地方。
    (1)使用JsonReader读Json字符串:
    [csharp] view plaincopy
    string jsonText =@"{""input"" : ""value"",""output"" : ""result""}";
    JsonReader reader = new JsonTextReader(newStringReader(jsonText));
    while (reader.Read())
    {
       Console.WriteLine(reader.TokenType + "		" + reader.ValueType+ "		" + reader.Value);
    }
    
    (2)使用JsonWriter写字符串:
    [csharp] view plaincopy
    StringWriter sw = new StringWriter();
    JsonWriter writer = new JsonTextWriter(sw);
    
    writer.WriteStartObject();
    writer.WritePropertyName("input");
    writer.WriteValue("value");
    writer.WritePropertyName("output");
    writer.WriteValue("result");
    writer.WriteEndObject();
    writer.Flush();
    
    string jsonText =sw.GetStringBuilder().ToString();
    Console.WriteLine(jsonText);
    
    (3)使用JObject读写字符串:
    [csharp] view plaincopy
    JObject jo = JObject.Parse(jsonText);
    string[] values =jo.Properties().Select(item => item.Value.ToString()).ToArray();
    
    (4)使用JsonSerializer读写对象(基于JsonWriter与JsonReader):
    数组型数据
    [csharp] view plaincopy
    string jsonArrayText1 ="[{'a':'a1','b':'b1'},{'a':'a2','b':'b2'}]";
    JArray ja =(JArray)JsonConvert.DeserializeObject(jsonArrayText1);
    string ja1a =ja[1]["a"].ToString();
    //或者
    JObject o = (JObject)ja[1];
    string oa = o["a"].ToString();
     

    Python:

    
    import json
    data= json.loads('{"ID": "2", "IP":"12.12.12.12", "Port": "3000"}')
    print data['ID']
    输出结果:"2"
    data = json.dumps(data)
    print data
    输出结果:{"ID": "2", "IP":"12.12.12.12", "Port": "3000"}
  • 相关阅读:
    LaTeX下的表格处理
    accumulate函数用法的坑
    将博客搬至CSDN
    linux; 文件名乱码;问价名出现问号
    vim打开多窗口、多文件之间的切换
    关于ssh-server
    更改Ubuntu gcc、g++默认编译器版本
    Elasticsearch nest实现类似Contains和Like功能
    Redis大幅性能提升之Batch批量读写
    Ext.NET 4.1 系统框架的搭建(后台) 附源码
  • 原文地址:https://www.cnblogs.com/webenh/p/7430772.html
Copyright © 2011-2022 走看看