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.

QA_A
  • 69
  • 4
  • 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(yourJSONString). This would make it easier for you to extract the appropriate data properties. To learn more about JSON you can refer to https://www.newtonsoft.com/json and https://www.w3schools.com/js/js_json_intro.asp – chri3g91 Feb 24 '22 at 08:08
  • Youve tagged this c#, but your code looks suspiciously java-like – Jamiec Feb 24 '22 at 08:16
  • duplicate: https://stackoverflow.com/questions/14640028/deserializing-json-into-string-array – chri3g91 Feb 24 '22 at 09:16

3 Answers3

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");
Serge
  • 40,935
  • 4
  • 18
  • 45
-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; }
}
chri3g91
  • 1,196
  • 14
  • 16
-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.

Dharman
  • 30,962
  • 25
  • 85
  • 135