JsonObject / JsonValue无法解析Windows Phone 8中的JSON [英] JsonObject/JsonValue is not working to parse JSON in Windows Phone 8

查看:69
本文介绍了JsonObject / JsonValue无法解析Windows Phone 8中的JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我在Windows Phone 8中解析JSON时遇到问题。我有一个json字符串,如

I'm facing issue while parsing JSON in windows phone 8. I've a json string like

{"ids":{"internal":[{"type":"ABCD","id":"7BCDCA0676CC8E2F5","time":19376,"asV":"AP_33160"}
                     ,{"type":"ADFP","id":"A2D3D22A6458BC2B","time":19376,"asV":"AP_420TETA"}
                     ]
            },
 "err":6000,
 "msg":"No Msg",
 "timeTL":87600
}



我想要的值如下:

I want to have values like:

string strIds= "{"internal":[{"type":"ABCD","id":"7BCDCA0676CC8E2F5","time":19376,"asV":"AP_33160"},{"type":"ADFP","id":"A2D3D22A6458BC2B","time":19376,"asV":"AP_420TETA"}]}" ;


int err= 6000;
string message= "msg";
int timeTL=87600;



我能够使用JsonValue在Windows 8上获得所需的结果,但Windows Phone 8不支持。主要问题是"内部"的内容不固定,我想将"ids"的完整值保存到字符串中。

I'm able to get the required results on Windows 8 using JsonValue but it is not supported on Windows Phone 8. The major problem is the content of "internal" is not fixed and I want to save the complete value of "ids" into a string.


谢谢!

Prabhjot Singh

Prabhjot Singh

推荐答案

我认为您可能会遇到有关预定义的"内部"问题。以下是您可以获得成功的示例数据;

I think you could get problem that is about "internal" which is predefined. Here is the sample below that you can get success of getting data;

using System.IO;
using System.Text;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;

您的自定义类用于json反序列化;

Your Custom class for json deserialization;

[DataContract]
        public class CustomJson
        {
            
            [DataMember]
            public _internal ids { get; set; }
            [DataMember]
            public int err { get; set; }
            [DataMember]
            public string msg { get; set; }
            [DataMember]
            public int timeTL { get; set; }

        }
        [DataContract]
        public class _internal
        {
            [DataMember]
            public internal_content[] @internal { get; set; }
        }
        [DataContract]
        public class internal_content
        {
            [DataMember]
            public string type { get; set; }
            [DataMember]
            public string id { get; set; }
            [DataMember]
            public int time { get; set; }
            [DataMember]
            public string asV { get; set; }

        }

序列化和反序列化的函数;

Your functions to serialization and deserialization;

   public string SerializeToJson(_internal _ids)
        {
            using (MemoryStream ms = new MemoryStream())
            {
                DataContractJsonSerializer serializer =
                        new DataContractJsonSerializer(typeof(_internal));
                serializer.WriteObject(ms, _ids);
                ms.Position = 0;

                using (StreamReader reader = new StreamReader(ms))
                {
                    return reader.ReadToEnd();
                }
            }
        }
        public CustomJson DeserializeToCustomJson(string jsonString)
        {
            using (MemoryStream ms = new MemoryStream(Encoding.Unicode.GetBytes(jsonString)))
            {
                DataContractJsonSerializer serializer =
                        new DataContractJsonSerializer(typeof(CustomJson));

                return (CustomJson)serializer.ReadObject(ms);
            }
        }
       




假设此处的主要代码  可以帮助您获取数据,你可以在你想要的地方使用这些;


Assume here is the primary codes  below that helps you to get data, you can these in where do you want;

 string json = "{\"ids\":{\"internal\":[{\"type\":\"ABCD\",\"id\":\"7BCDCA0676CC8E2F5\",\"time\":19376,\"asV\":\"AP_33160\"},{\"type\":\"ADFP\",\"id\":\"A2D3D22A6458BC2B\",\"time\":19376,\"asV\":\"AP_420TETA\"}]}, \"err\":6000, \"msg\":\"No Msg\", \"timeTL\":87600}";

            CustomJson cj = DeserializeToCustomJson(json);
            string strIds = SerializeToJson(cj.ids);
            //MessageBox.Show(strIds);
            int err = cj.err;
            string message = cj.msg;
            int timeTL = cj.timeTL;
            //MessageBox.Show(err.ToString());
            //MessageBox.Show(message);
            //MessageBox.Show(timeTL.ToString());




您可以添加System.Runtime.Serialization作为参考。



希望这些有帮助。


You could add System.Runtime.Serialization as Reference.

Hope these helps.


这篇关于JsonObject / JsonValue无法解析Windows Phone 8中的JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆