6

I am trying to parse a JSON string, that looks like that:

{
    "totalCreditsRemoved": 1,
    "invalidReceivers": [],
    "ids": [100070531],
    "validReceivers": ["+33635938286"]
}

I retrieve this from a web API, and stock it as a String:

var reader = new StreamReader(respStream);
String result = reader.ReadToEnd().Trim();
response = result;

Response is a public string

Then, in another method: I try to parse my json string:

var json = response;
var objects = JArray.Parse(json);

foreach (JObject root in objects)
{
    foreach (KeyValuePair<String, JToken> app in root)
    {
        totalCreditsRemoved = (String)app.Value["totalCreditsRemoved"];
        invalidReceivers = (String)app.Value["invalidReceivers"];
        ids = (String)app.Value["ids"];
        validReceivers = (String)app.Value["validReceivers"];
    }
}

But I always get this error:

JsonReaderException: Error reading JArray from JsonReader. Current JsonReader item is not an array: StartObject. Path '', line 1, position 1.

The error occurs at this line:

var objects = JArray.Parse(json);

I don't understand how to fix this ? Isn't OVHjson already an array Thanks in advance!

5
  • 4
    The response does not represent an array, so it can't be parsed by JArray.Parse. Commented Nov 6, 2017 at 10:39
  • 2
    Exactly what it says. From what you posted, the JSON is an object, not an array. Commented Nov 6, 2017 at 10:39
  • 1
    Use the correct method - if the content is an object use JObject.Parse. Better yet, create a DTO (ie a class with the properties you want) and deserialize the entire object at once, eg var myDTO = JsonConvert.DeserializeObject<MyDTO>(input); Console.WriteLine(myDTO.totalCredits); Commented Nov 6, 2017 at 10:43
  • looks like your api method is not consistent in the response format (sometimes it returns array, sometimes it returns object). That's a very kind of bad design I've encountered with when working with some partners. Commented Nov 6, 2017 at 10:44
  • I effectively receive an object, thanks Commented Nov 6, 2017 at 11:19

2 Answers 2

8

No need for JArray.Parse as it is not a array ..and is doing an overkill..hence the error..

var objects = JObject.Parse(json);

will do the job

and to extract

totalCreditsRemoved = (String)objects.Value["totalCreditsRemoved"];
invalidReceivers = (String)objects.Value["invalidReceivers"];
ids = (String)objects.Value["ids"];
validReceivers = (String)objects.Value["validReceivers"];
Sign up to request clarification or add additional context in comments.

Comments

0
 dynamic jsonObj = JsonConvert.DeserializeObject(responseFromServer);
 foreach (var obj in obj1.Properties())
 {
     if (obj.Name == "rewardConfirmation") {
         foreach (var obj2 in obj.Value)
         {
             MessageBox.Show(obj2.ToString());
         }
     }
 }

1 Comment

Welcome to SO, Please consider to add some explanation for your answer by editing it.

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.