导航:首页 > 编程语言 > cget请求json数据

cget请求json数据

发布时间:2021-02-28 04:32:48

Ⅰ C#怎么从http上返回jsON数据并读取

我之前做过网络和桌面应用的数据对接,你看看
/*
1、对象集合
*/
[DataContract]
publicclassItems
{
[DataMember]
publicList<ddd>ddd{get;set;}
publicItems()
{
ddd=newList<ddd>();
}

//把字符串转换为对象
publicstaticItemsFormJson(stringjson)
{
try
{
System.Runtime.Serialization.Json.DataContractJsonSerializera=newSystem.Runtime.Serialization.Json.DataContractJsonSerializer(typeof(Items));
using(MemoryStreamstream=newMemoryStream(Encoding.Unicode.GetBytes(json)))
{
return(Items)a.ReadObject(stream);
}
}
catch(Exception)
{

returnnull;
}
}
}
/*
简单的对象
*/
[DataContract]
publicclassddd
{
[DataMember]
publicintcid{get;set;}
[DataMember]
publicintpid{get;set;}
[DataMember]
publicintoid{get;set;}
[DataMember]
publicstringview_type{get;set;}
[DataMember]
publicstringstatus{get;set;}
[DataMember]
publicstringname{get;set;}
[DataMember]
publicstringeng_name{get;set;}
[DataMember]
publicstringctpl{get;set;}
[DataMember]
publicstringurl{get;set;}
}


/*
2、从HTTP请求中得到数据
*/

HttpWebRequestrequest=(HttpWebRequest)HttpWebRequest.Create("http://www.bursonchurch.net//index.php?Interface-index-keyt-123-act-catelist.html");
request.Timeout=5000;
request.Method="GET";
HttpWebResponseresponse=(HttpWebResponse)request.GetResponse();
Console.WriteLine("内容类型:"+response.ContentType);
Console.WriteLine("内容长度:"+response.ContentLength);
Console.WriteLine("服务器名:"+response.Server);
Console.WriteLine("资源的URI:"+response.ResponseUri);
Console.WriteLine("HTTP协议版本:"+response.ProtocolVersion);
Console.WriteLine("相应状态:"+response.StatusCode);
Console.WriteLine("相应方法:"+response.Method);
//头信息
for(inti=0;i<response.Headers.Count;++i)
{
Console.WriteLine(" HeaderName:{0},----------Value:{1}",response.Headers.Keys[i],response.Headers[i]);
}

StreamReadersr=newStreamReader(response.GetResponseStream());
stringjsonstr=sr.ReadToEnd();

//例子:假设得到的json数据下面序列化为对象
stringtest="{"ddd":[{"cid":"1","pid":"0","oid":"0","view_type":"0","status":"1","name":"西科推荐","eng_name":"","ctpl":"","ctitle":"","ckeywords":"","cdescription":"","url":"http:\/\/www.bursonchurch.net\/index.php?Cate-index-cid-1.html"},{"cid":"2","pid":"0","oid":"2","view_type":"0","status":"1","name":"校园活动","eng_name":"","ctpl":"","ctitle":"","ckeywords":"","cdescription":"","url":"http:\/\/www.bursonchurch.net\/index.php?Cate-index-cid-2.html"},{"cid":"3","pid":"0","oid":"2","view_type":"0","status":"1","name":"帅哥","eng_name":"","ctpl":"","ctitle":"","ckeywords":"动物世界keword","cdescription":"动物世界s","url":"http:\/\/www.bursonchurch.net\/index.php?Cate-index-cid-3.html"},{"cid":"4","pid":"0","oid":"4","view_type":"1","status":"1","name":"静物","eng_name":"","ctpl":"","ctitle":"","ckeywords":"","cdescription":"","url":"http:\/\/www.bursonchurch.net\/index.php?Cate-index-cid-4.html"},{"cid":"5","pid":"0","oid":"5","view_type":"1","status":"1","name":"美女","eng_name":"","ctpl":"","ctitle":"","ckeywords":"","cdescription":"","url":"http:\/\/www.bursonchurch.net\/index.php?Cate-index-cid-5.html"}]}";

//第一种转
javaScriptSerializerj=newJavaScriptSerializer();
Itemsii=newItems();
ii=j.Deserialize<Items>(jsonstr);


//第二种也行
Itemsit=Items.FormJson(jsonstr);

response.Close();
Console.ReadKey();

Ⅱ 如何使用c语言获取文件中的json数据

直接文件操作就行了。fopen,然后直接读出文件中的json数据,保存到一个数组里面就行了

Ⅲ C# 怎么读取到ajax异步过来的json的数据呢

JSON(JavaScript Object Notation)允许轻松地将 JavaScript 对象转换成可以随请求发送的数据(同步或异步都可以)。本文首先介绍JSON的数据格式,接着介绍如何在 JavaScript 中使用 JSON,重点介绍一下如何使用JSON完成数据的异步传输。 1. JSON的数据格式 a) 按照最简单的形式,可以用下面这样的 JSON 表示名称/值对: { "firstName": "Brett" } b) 可以创建包含多个名称/值对的记录,比如: { "firstName": "Brett", "lastName":"McLaughlin", "email": "[email protected]" } c) 可以创建值的数组 { "people": [ { "firstName": "Brett", "lastName":"McLaughlin", "email": "[email protected]" }, { "firstName": "Jason", "lastName":"Hunter", "email": "[email protected]" } ]} d) 当然,可以使用相同的语法表示多个值(每个值包含多个记录): { "programmers": [ { "firstName": "Brett", "lastName":"McLaughlin", "email": "[email protected]" }, { "firstName": "Jason", "lastName":"Hunter", "email": "[email protected]" } ], "authors": [ { "firstName": "Isaac", "lastName": "Asimov", "genre": "science fiction" }, { "firstName": "Tad", "lastName": "Williams", "genre": "fantasy" } ], "musicians": [ { "firstName": "Eric", "lastName": "Clapton", "instrument": "guitar" } ] } 注意,在不同的主条目(programmers、authors 和 musicians)之间,记录中实际的名称/值对可以不一样。JSON 是完全动态的,允许在 JSON 结构的中间改变表示数据的方式。 2. 在 JavaScript 中使用 JSON JSON 是 JavaScript 原生格式,这意味着在 JavaScript 中处理 JSON 数据不需要任何特殊的 API 或工具包。 2.1 将 JSON 数据赋值给变量 例如,可以创建一个新的 JavaScript 变量,然后将 JSON 格式的数据字符串直接赋值给它: var people = { "programmers": [ { "firstName": "Brett", "lastName":"McLaughlin", "email": "[email protected]" }, { "firstName": "Jason", "lastName":"Hunter", "email": "[email protected]" } ], "authors": [ { "firstName": "Isaac", "lastName": "Asimov", "genre": "science fiction" }, { "firstName": "Tad", "lastName": "Williams", "genre": "fantasy" } ], "musicians": [ { "firstName": "Eric", "lastName": "Clapton", "instrument": "guitar" } ] } 2.2 访问数据 将这个数组放进 JavaScript 变量之后,就可以很轻松地访问它。实际上,只需用点号表示法来表示数组元素。所以,要想访问 programmers 列表的第一个条目的姓氏,只需在 JavaScript 中使用下面这样的代码: people.programmers[0].lastName; 注意,数组索引是从零开始的。 2.3 修改 JSON 数据 正如访问数据,可以按照同样的方式修改数据: people.musicians[1].lastName = "Rachmaninov"; 2.4 转换回字符串 a) 在 JavaScript 中这种转换也很简单: String newJSONtext = people.toJSONString(); b) 可以将任何 JavaScript 对象转换为 JSON 文本。并非只能处理原来用 JSON 字符串赋值的变量。为了对名为 myObject 的对象进行转换,只需执行相同形式的命令: String myObjectInJSON = myObject.toJSONString(); 说明:将转换回的字符串作为Ajax调用的字符串,完成异步传输。 小结:如果要处理大量 JavaScript 对象,那么 JSON 几乎肯定是一个好选择,这样就可以轻松地将数据转换为可以在请求中发送给服务器端程序的格式。 3. 服务器端的 JSON 3.1 将 JSON 发给服务器 a) 通过 GET 以名称/值对发送 JSON 在JSON 数据中会有空格和各种字符,Web 浏览器往往要尝试对其继续编译。要确保这些字符不会在服务器上(或者在将数据发送给服务器的过程中)引起混乱,需要在JavaScript 的escape()函数中做如下添加: var url = "organizePeople.php?people=" + escape(people.toJSONString()); request.open("GET", url, true); request.onreadystatechange = updatePage; request.send(null); b) 利用 POST 请求发送 JSON 数据 当决定使用 POST 请求将 JSON 数据发送给服务器时,并不需要对代码进行大量更改,如下所示: var url = "organizePeople.php?timeStamp=" + new Date().getTime(); request.open("POST", url, true); request.onreadystatechange = updatePage; request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); request.send(people.toJSONString()); 注意:赋值时格式必须是var msg=eval('(' + req.responseText + ')'); 3.2 在服务器上解释 JSON a) 处理 JSON 的两步骤。 针对编写服务器端程序所用的语言,找到相应的 JSON 解析器/工具箱/帮助器 API。 使用JSON 解析器/工具箱/帮助器 API 取得来自客户机的请求数据并将数据转变成脚本能理解的东西。 b) 寻找 JSON 解析器 寻找JSON 解析器或工具箱最好的资源是 JSON 站点。如果使用的是 Java servlet,json.org 上的 org.json 包就是个不错的选择。在这种情况下,可以从 JSON Web 站点下载 json.zip 并将其中包含的源文件添加到项目构建目录。编译完这些文件后,一切就就绪了。对于所支持的其他语言,同样可以使用相同的步骤;使用何种语言取决于您对该语言的精通程度,最好使用您所熟悉的语言。 c) 使用 JSON 解析器 一旦获得了程序可用的资源,剩下的事就是找到合适的方法进行调用。

Ⅳ c如何解析json数据

你知道的逻辑结构吗?
JSONObjectjson=newJSONObject();
JSONArrayjsonMembers=newJSONArray();
for(Deptdaily:dailys){
JSONObjectmember=newJSONObject();
member.put("dept_name",daily.getDPT_NAME());
member.put("user_name",daily.getUser_name());
if(daily.getE_mail()==null){
daily.setE_mail("暂无");
}
member.put("email",daily.getE_mail());
member.put("daily_date",daily.getDaily_date());
jsonMembers.put(member);
}
JSONArrayjsonMembers1=newJSONArray();
JSONObjectmember1=newJSONObject();
member1.put("pagesize",dept.getPagesize());
member1.put("allpage",dept.getAllpage());
member1.put("allitems",dept.getAllitems());
member1.put("nowpage",dept.getNowpage());
jsonMembers1.put(member1);
json.put("check",jsonMembers);
json.put("page",jsonMembers1);
out.print(json);

Ⅳ 如何发送HTTP请求并取回一个JSON响应C ++升压

试试这个代码专属:Button show_data;JSONObject my_json_obj;String path,firstname,lastname;path = " http://192.168.101.123:255/services/services

Ⅵ C/C++实现WebService服务提供JSON数据的接口

1、C++可以实现webservice,这是毋庸置疑的.axis2本质是运行在tomcat下的一个servlet,分java版本,和C语言版本.官方网站为:http://axis.apache.org/,首页上写着:
The well known Apache Axis, and the the second generation of it, the Apache Axis2, are two Web Service containers that helps users to create, deploy, and run Web Services.Axis2 is avaialble in both Java as well as C, languages and details about each version can be found below. 大概意思就是这东西分java版本和C版本,可以方便用户创建,部署,运行web service.而C++完全是兼容C的.
2、需要服务器,要实现某个服务吧,至于怎样为其他平台服务,主要是监听端口实现解析http协议.js不需要拼串成XML,服务器才要拼串,JS是运行在客户端的,客户端也不是通过SOAP与服务端进行通讯的,而是根据需要调用的服务的WSDL,提供对应参数,客户端与服务端的通讯是用http协议的,而通讯方式根据是GET还是POST把相关参数放到HTTP头或者体中.而web service之间的通讯才可能用得到SOAP.
3、PHP调用web service是非常简单的,貌似有个函数通过SOAP调用.C++编写的web service肯定有WSDL,可以根据WSDL描述的端口参数,来调用.

安卓http请求 发送json数据

用utf8,应该不会乱码,有中文吗

Ⅷ 怎么用C语言获取JSON中的数据

用C语言获取JSON中的数据的方法是使用 CJSON。

以下简单介绍用CJSON的思路及实现:

1)创建json,从json中获取数据。

#nclude <stdio.h>

#include "cJSON.h"

char * makeJson()

{

cJSON * pJsonRoot = NULL;


pJsonRoot = cJSON_CreateObject();

if(NULL == pJsonRoot)

{

//error happend here

return NULL;

}

cJSON_AddStringToObject(pJsonRoot, "hello", "hello world");

cJSON_AddNumberToObject(pJsonRoot, "number", 10010);

cJSON_AddBoolToObject(pJsonRoot, "bool", 1);

cJSON * pSubJson = NULL;

pSubJson = cJSON_CreateObject();

if(NULL == pSubJson)

{

// create object faild, exit

cJSON_Delete(pJsonRoot);

return NULL;

}

cJSON_AddStringToObject(pSubJson, "subjsonobj", "a sub json string");

cJSON_AddItemToObject(pJsonRoot, "subobj", pSubJson);

char * p = cJSON_Print(pJsonRoot);

// else use :

// char * p = cJSON_PrintUnformatted(pJsonRoot);

if(NULL == p)

{

//convert json list to string faild, exit

//because sub json pSubJson han been add to pJsonRoot, so just delete pJsonRoot, if you also delete pSubJson, it will coremp, and error is : double free

cJSON_Delete(pJsonRoot);

return NULL;

}

//free(p);

cJSON_Delete(pJsonRoot);

return p;

}

void parseJson(char * pMsg)

{

if(NULL == pMsg)

{

return;

}

cJSON * pJson = cJSON_Parse(pMsg);

if(NULL == pJson)

{

// parse faild, return

return ;

}

// get string from json

cJSON * pSub = cJSON_GetObjectItem(pJson, "hello");

if(NULL == pSub)

{

//get object named "hello" faild

}

printf("obj_1 : %s ", pSub->valuestring);

// get number from json

pSub = cJSON_GetObjectItem(pJson, "number");

if(NULL == pSub)

{

//get number from json faild

}

printf("obj_2 : %d ", pSub->valueint);

// get bool from json

pSub = cJSON_GetObjectItem(pJson, "bool");

if(NULL == pSub)

{

// get bool from json faild

}

printf("obj_3 : %d ", pSub->valueint);

// get sub object

pSub = cJSON_GetObjectItem(pJson, "subobj");

if(NULL == pSub)

{

// get sub object faild

}

cJSON * pSubSub = cJSON_GetObjectItem(pSub, "subjsonobj");

if(NULL == pSubSub)

{

// get object from subject object faild

}

printf("sub_obj_1 : %s ", pSubSub->valuestring);

cJSON_Delete(pJson);

}


int main()

{

char * p = makeJson();

if(NULL == p)

{

return 0;

}

printf("%s ", p);

parseJson(p);

free(p);//这里不要忘记释放内存,cJSON_Print()函数或者cJSON_PrintUnformatted()产生的内存,使用free(char *)进行释放

return 0;

}

2)创建json数组和解析json数组

//创建数组,数组值是另一个JSON的item,这里使用数字作为演示

char * makeArray(int iSize)

{

cJSON * root = cJSON_CreateArray();

if(NULL == root)

{

printf("create json array faild ");

return NULL;

}

int i = 0;


for(i = 0; i < iSize; i++)

{

cJSON_AddNumberToObject(root, "hehe", i);

}

char * out = cJSON_Print(root);

cJSON_Delete(root);


return out;

}

//解析刚刚的CJSON数组

void parseArray(char * pJson)

{

if(NULL == pJson)

{

return ;

}

cJSON * root = NULL;

if((root = cJSON_Parse(pJson)) == NULL)

{

return ;

}

int iSize = cJSON_GetArraySize(root);

for(int iCnt = 0; iCnt < iSize; iCnt++)

{

cJSON * pSub = cJSON_GetArrayItem(root, iCnt);

if(NULL == pSub)

{

continue;

}

int iValue = pSub->valueint;

printf("value[%2d] : [%d] ", iCnt, iValue);

}

cJSON_Delete(root);

return;

}

Ⅸ 如何用c实现http post json

http是基于Socket通信的一种通信规约,post是http规约的一种功能,json是常用于字符串解释型编程语言及一些脚本上用的对象格式。

Ⅹ 用$.getJSON方法取数据,怎么解读返回值

|

多层的继续在后面添加即可

例如:

{
info:{
user:{
name:'name',
id:'id',
}
}
}

获取回name值的话就这样答data.info.user.name||data['info']['user']['name']
阅读全文

与cget请求json数据相关的资料

热点内容
团鬼六电影 浏览:290
vip免费网站全免费 浏览:257
夏荷vs秋凝乳斗 浏览:420
苹果下载打不开怎么回事 浏览:765
刘凡菲地下车库什么电影 浏览:507
爱情动作电影名字 浏览:917
穿越雪域雄鹰之战神系统 浏览:233
关于越南缅甸的电影 浏览:436
朴银狐同类型演员 浏览:762
js时间保留2位 浏览:608
午马神电影 浏览:257
现代师徒训诫罚跪严苛 浏览:374
金花媛代表作 浏览:685
稚气by嗜酒吃茶txt下载 浏览:932
看免费大片网站 浏览:915
手机文件夹里哪些是没用的 浏览:802
李彩谭最新电影 浏览:226
尹雪熙的电影有什么 浏览:229
一个电视剧男主角叫蛋蛋 浏览:561
能看的网址在线 浏览:393

友情链接