0

I have a c# function

 public JObject MessageJSON(string dv, string sip)
            {

                JArray details = new JArray();

                //Gr 1
                JArray detail = new JArray();

                JObject detailR = new JObject();
                detailR["dt"] = "int";
                detailR["dn"] = "status";
                detailR["dv"] = dv;

                detail.Add(detailR);

                //Gr 2
                JObject issuerobj = new JObject();
                issuerobj.Add("sid", "2");
                issuerobj.Add("sip", sip);
                issuerobj.Add("storeid", 9999);
                issuerobj.Add("timestamp", 1390236777);


                //list of events

                JObject objectJS = new JObject();
                objectJS.Add(detail);
                objectJS.Add(issuerobj);


                JArray ret = new JArray();
                ret.Add(objectJS);

                JObject stringJSON = new JObject();
                stringJSON.Add(ret);

                return stringJSON;

            }

and I need from function to return a JSON like this:

{ "events" : [ { "event" : { "details" : [ { "dn" : "status",
                  "dt" : "int",
                  "dv" : 2
                } ],
            "issuer" : { "sid" : 2,
                "sip" : "192.168.1.125",
                "storeid" : 9999,
                "timestamp" : 1390236777
              },
            "type" : "VRA3.5_SCREEN"
          } } ] }

One of the problem is that I have error when I try to add list to object.

Advanced thanks

3
  • 1
    Which error on what line... Commented Jan 21, 2014 at 15:28
  • Can not add Newtonsoft.Json.Linq.JArray to Newtonsoft.Json.Linq.JObject. at line no.27 Commented Jan 21, 2014 at 15:29
  • Better edit your question with that info, nobody likes to count line numbers (eg. put comment on the line where the exception is happening) Commented Jan 21, 2014 at 15:43

1 Answer 1

1

Change

objectJS.Add(detail);
objectJS.Add(issuerobj);

To

objectJs["details"] = detail;
objectJs["issuer"] = issuerobj;

and stringJSON.Add(ret) to stringJSON["events"] = ret

Sign up to request clarification or add additional context in comments.

1 Comment

Not yet. at the line "objectJS.Add(detail);" will broke my code

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.