導航:首頁 > 編程語言 > 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數據相關的資料

熱點內容
怎麼用ps把臉p黑教程 瀏覽:223
播放iso藍光原盤app有哪些 瀏覽:115
尋呼機地址碼編程是什麼意思 瀏覽:947
數據包安全未知為什麼解壓不出來 瀏覽:69
愛人女主角 瀏覽:623
古龍小說下載 瀏覽:242
linux單詞統計 瀏覽:200
iphone音符符號 瀏覽:649
女設計師幾百年不死韓國 瀏覽:245
linux無法生成gbk文件 瀏覽:590
免費的最新電影qq群 瀏覽:83
數控g76螺紋怎麼編程 瀏覽:779
哪個影院不需要VIP 瀏覽:706
百度分享代碼錯誤 瀏覽:920
酷狗網路列表恢復 瀏覽:149
免費天堂網站 瀏覽:667
玉器網站源碼 瀏覽:249
開辟內宇宙超脫的小說 瀏覽:242
第二書包荷包 瀏覽:711
qq什麼版本有辦公應用 瀏覽:815

友情鏈接