0

I want to build a json like this:

...
  "conditions": [
      [
        "Language",
        "IN",
        [
          "en"
        ]
      ]
    ]
...

I am using this code:

new JProperty("conditions", new JArray(new JArray((new JArray("Language", "IN", new JArray(IsEnglish ? "en" : "es"))))))

But this one built me a json with one less array.

  "conditions": 
      [
        "Language",
        "IN",
        [
          "en"
        ]
      ]

I tried to nest this into another JArray but that is not working. Please suggest.

EDIT: This is the whole Json making statement

 JObject json = new JObject(
           new JProperty("application", pwApplication),
           new JProperty("auth", pwAuth),
           new JProperty("notifications",
               new JArray(
                   new JObject(
                       new JProperty("send_date", "now"),
                       new JProperty("content", new JObject(new JProperty("en", pushContentEnglish), new JProperty("es", pushContentSpanish))),
                       new JProperty("ios_badges", 0),
                       new JProperty("data", new JObject(new JProperty("custom", new JObject(new JProperty("t", notificationType), new JProperty("i", objectId))))),
                       new JProperty("devices", new JArray(strDeviceArray)),
                       new JProperty("conditions", new JArray(new JArray((new JArray("Language", "IN", new JArray(IsEnglish ? "en" : "es")))))
                       )))));
5
  • downvoter, care to comment why? Commented Jul 24, 2014 at 11:53
  • 1
    How many data do you using when you trying to do this ? I think you using only one data, and the lib, don't understand that you want to make one more array... ? Commented Jul 24, 2014 at 11:54
  • @BrunoCasali yes thats the whole point..how to make the lib create one more array Commented Jul 24, 2014 at 11:56
  • 1
    Can you use Newtonsoft (Json.net)? That would save you a whole heap of code in this case. Commented Jul 24, 2014 at 12:00
  • @zaitsman already using the same Commented Jul 24, 2014 at 12:01

3 Answers 3

1

This seems to work:

new JProperty("conditions", new JArray((JContainer)new JArray("Language", "IN", new JArray(IsEnglish ? "en" : "es"))))

And the reason this works is because the JArray(JArray) constructor copies the elements from the JArray parameter to the JArray being constructed, whereas casting it to JContainer (from which JArray inherits) invokes the JArray(Object) overload.

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

Comments

1

If you're using Json.Net, why not do:

var conditions = new[] { new object[] { "Language", "IN", new[] { "en" } } };
var result = "conditions:" + JsonConvert.SerializeObject(conditions);

2 Comments

thanks for the answer..but how to add this property to my main json
@Lakshay, can you update the post with the entire result that you're looking to achieve (e.g. full format of the resulting json)
0

I think do you using only one data...

"conditions": [
  [
    "Language",
    "IN",
    [
      "en"
    ]
  ]
]

but if you try to make one more...

"conditions": [
    [
        "Language",
        "IN",
        [
            "en"
        ]
    ],
    [
        "Language",
        "IN",
        [
            "pt"
        ]
    ]
]

If you can test this code with more than one data in this expected array... ? You'll tried this?

2 Comments

@Lakshay, understand now ?!
i understand your point..but the stupid API wants the data to be a single item in an array..

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.