Unity3D 中使用Json,以及web php 交互

using LitJson;

访问web url 方法;

IEnumerator getText()
  {
      // Security.PrefetchSocketPolicy("127.0.0.1", 843);  
      string url = host_name;
      WWW www = new WWW(url);
      yield return www;
      print(www.text); //得到 json_str,从web上得到json 语句
  }

 

将 json str 转成对象

JsonData jd;

jd = JsonMapper.ToObject(json_str);

string s = jd.ToJson();
print("s=" + s);

          for (int i = 0; i < jd.Count; i++)
          {
              String pid = jd[i]["pid"].ToString();
              String pic_url = jd[i]["pic"].ToString();
        }

 

在c#中生成json,然后提交经web php,实现向 mysql里添加数据

JsonData data = new JsonData();
           for (int i = 0; i < model_obj.Length; i++)
           {
               JsonData data2 = new JsonData();
               if (model_obj[i] != null)
               { 
                  data2["name"] = "name" + i;
               }
              data[ i.ToString() ] = data2;
           }

           string post_str = data.ToJson();
           print("post_str = " + post_str);

           Uri address = new Uri( );
           WebClient client = new WebClient();
           NameValueCollection paramToPass = new NameValueCollection();

           paramToPass.Add("str", post_str);

           byte[] responseArray = client.UploadValues(address, "POST", paramToPass);
           print("\nResponse received was : " +  Encoding.ASCII.GetString(responseArray));

为了使用 NameValueCollection ,要添加

using System.Collections;
using System.Collections.Specialized;

参考 http://www.cnblogs.com/peiandsky/archive/2012/04/20/2459219.html

posted on 2012-11-23 22:14  tokutoku  阅读(478)  评论(0编辑  收藏  举报

导航