[英]Parse Json_encoded array from PHP in C# UWP
I developing a UWP app using C#, this conect a PHP WebService, this web service return a array encoded using json_encode function, i cant parse this json string in my UWP app, please help我使用 C# 开发了一个 UWP 应用程序,这连接了一个 PHP WebService,这个 Web 服务返回一个使用 json_encode 函数编码的数组,我无法在我的 UWP 应用程序中解析这个 json 字符串,请帮忙
1: PHP CODE 1:PHP代码
$aCli[]= array("CAT"=>"OK","MSG"=>"SESION-OK","EXTRA"=>array("ID"=>"$spID","NOM"=>"$spNom"));
echo json_encode($aCli);
2: The result using postman is: 2:使用postman的结果是:
[{"CAT":"OK","MSG":"SESION-OK","EXTRA":{"ID":"3","NOM":"CHARLS"}}]
3. The result using Async Task from C# is: 3. 从 C# 使用 Async Task 的结果是:
"[{\"CAT\":\"OK\",\"MSG\":\"SESION-OK\",\"EXTRA\":{\"ID\":\"3\",\"NOM\":\"CHARLS\"}}]"
4. How to deserialize this string?, i am trying it using 4.如何反序列化这个字符串?,我正在尝试使用
using Windows.Data.Json;
5. This is the code using in this time 5.这是这次使用的代码
sJSON= await IniciarSesion();//this use async class to connect with webservice
JsonObject objJson = JsonObject.Parse(sJSON);//error is raised in this line
/*Json String is invalid*/
sCat = objJson["CAT"].GetString();
sMsg = objJson["MSG"].GetString();
Important comment above:以上重要评论:
your JSON is a representation of an array, not an object.你的 JSON 是一个数组的表示,而不是一个对象。
IF you can use Newtonsoft (JSON.Net) , here's one way, with dependency on JSON.Net
, not Windows.Data.Json
.如果您可以使用Newtonsoft (JSON.Net) ,这是一种方法,依赖于JSON.Net
,而不是Windows.Data.Json
。 Trivial sample only, improve as necessary (null checks, etc.)仅微不足道的样本,根据需要改进(空检查等)
using System;
using Newtonsoft.Json.Linq;
public class Program
{
public static void Main()
{
var str = "[{\"CAT\":\"OK\",\"MSG\":\"SESION-OK\",\"EXTRA\":{\"ID\":\"3\",\"NOM\":\"CHARLS\"}}]";
var j = JArray.Parse(str);
var token = j[0];
//using dynamic to simplify sample, create/use your own type
var obj = token.ToObject<dynamic>();
Console.WriteLine(obj.CAT);
Console.WriteLine(obj.MSG);
Console.WriteLine(obj.EXTRA);
Console.WriteLine(obj.EXTRA.ID);
Console.WriteLine(obj.EXTRA.NOM);
}
}
Hth..嗯..
You could use Newtonsoft to deserialize JSON string directly.您可以使用Newtonsoft直接反序列化 JSON 字符串。 For your requirement, you need to make the data model fist.根据您的要求,您需要制作数据模型。
[{"CAT":"OK","MSG":"SESION-OK","EXTRA":{"ID":"3","NOM":"CHARLS"}}] [{"CAT":"OK","MSG":"SESION-OK","EXTRA":{"ID":"3","NOM":"CHARLS"}}]
public class Pet
{
public string Cat { get; set; }
public string Msg { get; set; }
public Extra Extra { get; set; }
}
public class Extra
{
public string Id { get; set; }
public string Nom { get; set; }
}
Usage用法
private void Button_Click(object sender, RoutedEventArgs e)
{
var str = "[{\"CAT\":\"OK\",\"MSG\":\"SESION-OK\",\"EXTRA\":{\"ID\":\"3\",\"NOM\":\"CHARLS\"}}]";
var items = JsonConvert.DeserializeObject<List<Pet>>(str);
}
For more you could refer Serializing and Deserializing JSON documentation.有关更多信息,您可以参考 序列化和反序列化 JSON文档。
The problem is with the brackets ' [] ' at the beginning and end of the string,问题在于字符串开头和结尾的括号“[]”,
You should trim them like:你应该像这样修剪它们:
' YOUR_JSON_STRING = YOUR_JSON_STRING.trim('[',']'); ' YOUR_JSON_STRING = YOUR_JSON_STRING.trim('[',']');
and then parse the json string below:然后解析下面的json字符串:
' dynamic result = JsonConvert.DeserializeObject(YOUR_JSON_STRING)' '动态结果 = JsonConvert.DeserializeObject(YOUR_JSON_STRING)'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.