Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8980683
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T20:08:55+00:00 2026-06-15T20:08:55+00:00

I’m using the json.net (Newtonsoft) library to parse an API json response. I only

  • 0

I’m using the json.net (Newtonsoft) library to parse an API json response. I only need the id values and the rank values. The json looks like this:

{"items":
    [
        {"fields":
            {"entry":
                [
                    {"key":"Customer","value":"ABC Electronics"},
                    {"key":"Status","value":"Untouched"},
                    {"key":"Amount","value":"$1000"},
                    {"key":"rank","value":"3.0"}
                ]
            },
            "id":"1001",
            "owner":"dave@sales123.com"
        },
        {"fields":
            {"entry":
                [
                    {"key":"Customer","value":"General Products"},
                    {"key":"Status","value":"Developing"},
                    {"key":"Amount","value":"$6000"},
                    {"key":"rank","value":"6.0"}
                ]
            },
            "id":"1002",
            "owner":"john@sales123.com"
        }
    ]
}

I have a method inside my class that makes the request, loops through the json and grabs the id and rank, then adds those values as a new RankScore object to a List class member. My question is if there is a better way to find the “value” where the key == “rank” in the entry array than just looping through it and checking each key. Any LINQ way that I’ve been unable to find?

public void MakeRequest(apiURL) {
    var requestUrl = new Uri(apiUrl, "/ranks/0");
    HttpWebRequest request = WebRequest.Create(requestUrl) as HttpWebRequest;
    request.Credentials = new NetworkCredential(this.Username, this.Password);

    using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
    {
        using (var reader = new StreamReader(response.GetResponseStream()))
        {
            var token = JObject.Parse(reader.ReadToEnd());
            var items = token["items"];
            foreach (var item in items)
            {
                var id = item.Value<string>("id");
                var entries = item["fields"]["entry"];
                string rank = null;

                // **** Is there a better way to do this? ****
                foreach (var entry in entries) 
                {
                    if (entry.Value<string>("key") == "rank")
                    {
                        rank = entry.Value<string>("value");
                        break;
                    }
                }
                var ranking = new RankScore(int.Parse(id), rank);
                this.Ranks.Add(ranking);
            }

        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T20:08:56+00:00Added an answer on June 15, 2026 at 8:08 pm

    Simple answer, no. You could let something else do some of the looping for you but it’s not like you’re going to come up with a solution that performs better by using LINQ (it’s also just looping over the collection).

    If your goal is simply to clean up your code you could serialize into an object, then you’ll have a simpler loop in your code (of course json.NET will be doing plenty of looping);

    for (int i = 0; i < Obj.items.Length; i++)
    {
        Obj.items[i].id //do something with this here
    }
    

    Although I personally wouldn’t recommend that. I like the simple method. Another (stupid) idea would be to use RegEx. You would have fewer lines but much worse performance. Basically, you’re not going to remove iteration, you can use an abstraction to do some of it but there’s really no point. I would just stick with what you have unless you’re going to do a lot more with the json. If you intend to do more with the json elsewhere then I would serialize into an object.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using the SimpleRSS gem to parse a WordPress RSS feed. The only
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I would like to run a str_replace or preg_replace which looks for certain words
I have thousands of HTML files to process using Groovy/Java and I need to
I'm making a simple page using Google Maps API 3. My first. One marker
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am reading a book about Javascript and jQuery and using one of the
link Im having trouble converting the html entites into html characters, (&# 8217;) i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.