0

I know its an array, but I am completely new to JSON and need help comprehending how this is structured, here is my attempt at extracting data:

String JSonString = readURL("//my URL is here");
JSONArray s = JSONArray.fromObject(JSonString);
JSONObject Data =(JSONObject)(s.getJSONObject(0));
System.out.println(Data.get("RecycleSiteUrl"));

I want to extract RecycleSiteUrl based on SiteId

My JSON data that I have goes like this :

 [
    {
    "SiteId": 1,
    "RecycleLogoUrl": "https://static-contrado.s3-eu-west- 1.amazonaws.com/cms/recyclecarelabel/d867c499-abc0-4ade-bc1a-f5011032c3e0132901511939451201.jpeg",
    "RecycleSiteUrl": "bagsoflove.co.uk/recycling",
    "Culture": "en-GB"
    },
    {
    "SiteId": 10,
    "RecycleLogoUrl": "https://static-contrado.s3-eu-west-1.amazonaws.com/cms/recyclecarelabel/95d28588-33e3-420c-8b24-4a8095c0f6ac132901511364264751.jpeg",
    "RecycleSiteUrl": "contrado.co.uk/recycling",
    "Culture": "en-GB"
    }]

I dont really have a strong grasp of this stuff so all the help is appreciated.

3
  • 1
    Use Newtownsoft.json namespace. E.g. install it through Nuget. This way you can de-serialize it to a class object utilizing JsonConvert.DeserializeObject<YourObject>(yourJSONString). This would make it easier for you to extract the appropriate data properties. To learn more about JSON you can refer to newtonsoft.com/json and w3schools.com/js/js_json_intro.asp Commented Feb 24, 2022 at 8:08
  • Youve tagged this c#, but your code looks suspiciously java-like Commented Feb 24, 2022 at 8:16
  • duplicate: stackoverflow.com/questions/14640028/… Commented Feb 24, 2022 at 9:16

3 Answers 3

0

you can try this, it doesn't need to create any classes

var jsonParsed=JArray.Parse(json);
var siteId=10;
    
var recycleSiteUrl  = GetRecycleSiteUrl(jsonParsed,siteId); // contrado.co.uk/recycling

public string GetRecycleSiteUrl(JArray jArray, int siteId)
{
    return jArray.Where(x=> (string) x["SiteId"] == siteId.ToString()).First()["RecycleSiteUrl"].ToString();
}

or using json path

string recycleSiteUrl= (string)jsonParsed
.SelectToken("$[?(@.SiteId=="+siteId.ToString()+")].RecycleSiteUrl");
Sign up to request clarification or add additional context in comments.

Comments

-1
using Newtonsoft.Json;  
using System.Linq;  

public void Method()
{
   var yourSearchParameter = 10;
   string jsonStr = readURL("//my URL is here");
   var obj = JsonConvert.DeserializeObject<List<RecycleSite>>(jsonStr);
   var siteUrl = obj.SingleOrDefault(q => q.SiteId == yourSearchParameter).RecycleSiteUrl;
 /* Do something with the siteUrl  */
}

public class RecycleSite
{
   public int SiteId { get; set; }
   public string RecycleLogoUrl { get; set; }
   public string RecycleSiteUrl { get; set; }
   public string Culture{ get; set; }
}

Comments

-2
using Newtonsoft.Json;
using System;
using System.Collections.Generic;

public Class Site
{
    public string GetRecycleSiteUrl(int siteId, string jsonArray)
    {
        var siteInfos = JsonConvert.DeserializeObject<List<SiteInfo>>(jsonArray);
        string recycleSiteUrl = siteInfos.FirstOrDefault(info => info.SiteId == siteId).RecycleSiteUrl;
        return recycleSiteUrl;
    }
}

public class SiteInfo
{
    public int SiteId { get; set; }
    public string RecycleLogoUrl { get; set; }
    public string RecycleSiteUrl { get; set; }
    public string Culture { get; set; }
}

You can create Site object and access GetRecycleSiteUrl method by passing respective parameter values.

Comments

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.