2016-04-30 83 views
0

我能以某種方式訂購通過httprequest檢索的JSON,還是隻能在後端執行此操作?Xamarin C#,我可以排序/訂購通過httpclient收集的JSON列表嗎?

我通過以下代碼收集數據:我可以在我的http地址之後添加「?order = info1」行(info1是我的json中的值)嗎?

static public async Task<JObject> getInfo() 
    { 
     var httpClientRequest = new HttpClient(); 

     var result = await httpClientRequest.GetAsync ("http://localhost/information.php"); //can I add: "?order=info1" somehow? 

     var resultString = await result.Content.ReadAsStringAsync(); 

     var jsonResult = JObject.Parse (resultString); 

     return jsonResult; 

    } 

我的JSON是這樣的:

{ 
    status: "ok", 
    records: [ 
    { 
     info1: "test1", 
     id: "78" 
    }, 
    { 
     info1: "test2", 
     id: "79" 
    }, 
    { 
     info1: "test3", 
     id: "80" 
    } 
    ] 
    } 

更新的代碼:

static public async Task<JObject> getInfo() 
    { 
     var httpClientRequest = new HttpClient(); 

     var result = await httpClientRequest.GetAsync ("http://localhost/information.php"); 

     var resultString = await result.Content.ReadAsStringAsync(); 

     var jsonResult = JObject.Parse (resultString); 

     JArray sorted = new JArray(jsonResult["records"].OrderBy(obj => obj["info1"])); 

     jsonResult["records"] = sorted; 
     return jsonResult; 

    } 

回答

2

您應該能夠通過這樣的records數組進行排序:

// sort by "info1" property 
JArray sorted = new JArray(jsonResult["records"].OrderBy(obj => obj["info1"])); 

jsonResult["records"] = sorted; 
+0

找不到 「.OrderBy」 – medvedo

+0

@medvedo'使用System.Linq' – Eser

+0

似乎不工作。代碼運行順利,但我收集的信息進入應用程序相同。 json保持不變。更新與解決方案,使其不工作 – medvedo