json 无法在C# .NET中分析对象

ifsvaxew  于 2023-02-06  发布在  C#
关注(0)|答案(4)|浏览(166)

我无法在C#. NET中分析以下对象。我收到分析对象错误。

messageBody(字符串格式-我正在获取字符串格式的以下对象)

{
  "Type" : "Nti_1",
  "MessageId" : "c1b7cd5b-3099-532e-9539-91376eea607b",
  "SequenceNumber" : "10000000000000128000",
  "TopicArn" : "arn:aws:sns:us-east-1:xxxxxxx:Project1-SNS.fifo",
  "Message" : "{'Prop1':'202020','Prop2':'Hi-I again reached','Prop3':'Testing String'}",
  "Timestamp" : "2023-02-05T07:35:15.905Z",
  "UnsubscribeURL" : "https://sns.us-east-1.amazonaws.com/?Action=Unsubscribe&SubscriptionArn=arn:aws:sns:us-east-1:xxxxxx:PushNotification-SNS.fifo:08d0fac2-ac0f-4ff9-b583-61024a98672d",
  "MessageAttributes" : {
    "EventType" : {"Type":"String.Array","Value":"["SMS","ArialRoot"]"}
  }
}

下面是为解析上述对象而创建的类

public class ParentObject
{
    public string Type { get; set; }
    public string MessageId { get; set; }
    public string SequenceNumber { get; set; }

    public string TopicArn { get; set; }

    public ChildObject Message { get; set; }
    public string Timestamp { get; set; }

    public string UnsubscribeURL { get; set; }

    public string MessageAttributes { get; set; }
}

public class ChildObject
{
    public string Prop1 { get; set; }
    public string Prop2 { get; set; }
    public string Prop3 { get; set; }
}

下面是我试图解析对象的代码

ParentObject obj = JsonConvert.DeserializeObject<ParentObject>(messageBody)

我不知道我错过了什么解析字符串指定上述对象。

dxxyhpgq

dxxyhpgq1#

你的json是无效的,你可以使用替换字符串来修复它

messageBody = messageBody.Replace("\"[","[").Replace("]\"","]");

还有你的消息属性是字符串,如果你想让它json你也必须修正它

messageBody = messageBody.Replace("'", "\"").Replace("\"{", "{").Replace("}\"", "}");
ParentObject obj = JsonConvert.DeserializeObject<ParentObject>(messageBody);

也可以使用以下代码修复Message属性

JObject jObj = JObject.Parse(messageBody);
jObj["Message"] = JObject.Parse((string) jObj["Message"]);
ParentObject obj = jObj.ToObject<ParentObject>();

我也修复了类

public class ParentObject
{
    public string Type { get; set; }
    public string MessageId { get; set; }
    public string SequenceNumber { get; set; }
    public string TopicArn { get; set; }
    public Message Message { get; set; }
    public DateTime Timestamp { get; set; }
    public string UnsubscribeURL { get; set; }
    public MessageAttributes MessageAttributes { get; set; }
}
public class EventType
{
    public string Type { get; set; }
    public List<string> Value { get; set; }
}

public class MessageAttributes
{
    public EventType EventType { get; set; }
}

public class Message
{
    public string Prop1 { get; set; }
    public string Prop2 { get; set; }
    public string Prop3 { get; set; }
}

但如果将源代码中的消息体固定为该消息体,则效果会更好

{
  "Type" : "Nti_1",
  "MessageId" : "c1b7cd5b-3099-532e-9539-91376eea607b",
  "SequenceNumber" : "10000000000000128000",
  "TopicArn" : "arn:aws:sns:us-east-1:xxxxxxx:Project1-SNS.fifo",
  "Message" : {"Prop1":"202020","Prop2":"Hi-I again reached","Prop3":"Testing String"},
  "Timestamp" : "2023-02-05T07:35:15.905Z",
  "UnsubscribeURL" : "https://sns.us-east-1.amazonaws.com/?Action=Unsubscribe&SubscriptionArn=arn:aws:sns:us-east-1:xxxxxx:PushNotification-SNS.fifo:08d0fac2-ac0f-4ff9-b583-61024a98672d",
  "MessageAttributes" : {
    "EventType" : {"Type":"String.Array","Value":["SMS","ArialRoot"]}
  }
}
knpiaxh1

knpiaxh12#

您需要创建一个与JSON结构匹配的C#类

using Newtonsoft.Json;

public class MyClass
{
    public string Type { get; set; }
    public string MessageId { get; set; }
    public string SequenceNumber { get; set; }
    public string TopicArn { get; set; }
    public string Message { get; set; }
    public string Timestamp { get; set; }
    public string UnsubscribeURL { get; set; }
    public MessageAttributes MessageAttributes { get; set; }
}

public class MessageAttributes
{
    public EventType EventType { get; set; }
}

public class EventType
{
    public string Type { get; set; }
    public string Value { get; set; }
}

反序列化JSON字符串

MyClass obj = JsonConvert.DeserializeObject<MyClass>(json);
brvekthn

brvekthn3#

MessageAttributes在您的.net类型中是字符串,在您的json中是对象。

nc1teljy

nc1teljy4#

试试这个

var parent = JsonConvert.DeserializeObject<ParentObject>(messageBody);
var child = JsonConvert.DeserializeObject<ChildObject>(parent.Message);
parent.Message = child;

相关问题