第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

從 C# 桌面應(yīng)用程序中的 json 字符串響應(yīng)中獲取數(shù)據(jù)

從 C# 桌面應(yīng)用程序中的 json 字符串響應(yīng)中獲取數(shù)據(jù)

C#
慕桂英3389331 2022-11-13 14:43:29
我有來(lái)自服務(wù)器的多個(gè)學(xué)生姓名的響應(yīng){"ENVELOPE":{"STUDENTLIST":{"STUDENT":["John","HHH"]}}}或單個(gè)學(xué)生姓名{"ENVELOPE":{"STUDENTLIST":{"STUDENT":"John"}}}如果有錯(cuò)誤{"RESPONSE":{"LINEERROR":"Could not find Students"}}從這些回復(fù)中,如果沒(méi)有錯(cuò)誤,我想要學(xué)生姓名數(shù)組 else string with error ie string[] names = {"John","HHH"} or string[] names = {"John"} else string error = "Could not find學(xué)生”;我試過(guò) JObject jObj = JObject.Parse(responseFromServer); var msgProperty = jObj.Property("ENVELOPE"); var respProperty = jObj.Property("RESPONSE"); //check if property exists if (msgProperty != null) {     var mag = msgProperty.Value;     Console.WriteLine("has Student : " + mag);     /*       need logic here :/ */ }                 else if (respProperty != null) {     Console.WriteLine("no Students"); } else {      Console.WriteLine("Error while getting students");                     }希望你得到這個(gè)..
查看完整描述

1 回答

?
繁華開(kāi)滿(mǎn)天機(jī)

TA貢獻(xiàn)1816條經(jīng)驗(yàn) 獲得超4個(gè)贊

通常我會(huì)建議在模型中反序列化你的對(duì)象,但由于該STUDENT屬性有時(shí)是一個(gè)數(shù)組,有時(shí)是一個(gè)字符串,不幸的是,這很麻煩。我建議反序列化您收到的實(shí)際 xml,因?yàn)槲蚁M?xml 模型更容易處理。

與此同時(shí),這將起作用:

string input = "{\"ENVELOPE\":{\"STUDENTLIST\":{\"STUDENT\":[\"John\",\"HHH\"]}}}";

// string input = "{\"ENVELOPE\":{\"STUDENTLIST\":{\"STUDENT\":\"John\"}}}";

// string input = "{\"RESPONSE\":{\"LINEERROR\":\"Could not find Students\"}}";


JObject jObj = JObject.Parse(input);

if (jObj["RESPONSE"] != null)

{

    string error = jObj["RESPONSE"]["LINEERROR"].ToString();

    Console.WriteLine($"Error: {error}");


    // or throw an exception

    return;

}


var studentNames = new List<string>();


// If there is no error, there should be a student property.

var students = jObj["ENVELOPE"]["STUDENTLIST"]["STUDENT"];

if (students is JArray)

{

    // If the student property is an array, add all names to the list.

    var studentArray = students as JArray;

    studentNames.AddRange(studentArray.Select(s => s.ToString()));

}

else

{

    // If student property is a string, add that to the list.

    studentNames.Add(students.ToString());

}


foreach (var student in studentNames)

{

    // Doing something with the names.

    Console.WriteLine(student);

}


查看完整回答
反對(duì) 回復(fù) 2022-11-13
  • 1 回答
  • 0 關(guān)注
  • 143 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)