1

First of all, i am new to this so please pardon me. Have been working on a music app and I am trying to parse JSON code from a streaming link and display "artist" name and "title" of song to my app users. But i am having issues collecting the data. Here is my JSON code from the streaming link:

{"type":"result","data":[{"title":"My Stream ","song":"Unknown - The Authorised One","track":{"artist":"Unknown Artist","title":"The Authorised One","album":"Unknown","royaltytrackid":181938.0000,"started":1498151105,"id":181938,"length":0,"playlist":{"id":3520,"title":"Rev Arome E. Adah"},"buyurl":"https:\/\/itunes.apple.com\/us\/album\/the-unknown-god\/id772022436?uo=4","imageurl":"http:\/\/is5.mzstatic.com\/image\/thumb\/Music5\/v4\/d7\/6d\/52\/d76d52df-db43-7130-0e37-62241ff50a21\/source\/100x100bb.jpg"},"bitrate":"128 Kbps","server":"Online","autodj":"Online","source":"Yes","offline":false,"summary":"<a href=\"http:\/\/cp9.serverse.com:2199\/tunein\/-stream\/svhxmwhp.pls\">Eloti Designs Stream - Unknown - The Authorised One<\/a>","listeners":0,"maxlisteners":1000,"reseller":0,"serverstate":true,"sourcestate":true,"sourceconn":1,"date":"Jun 22, 2017","time":"07:06 PM","rawmeta":"Unknown - The Authorised One ","mountpoint":"\/stream","tuneinurl":"http:\/\/209.133.216.3:7550\/stream","directtuneinurl":"","proxytuneinurl":"http:\/\/209.133.216.3\/proxy\/svhxmwhp?mp=\/stream","tuneinformat":"mp3","webplayer":"muses","servertype":"ShoutCast2","listenertotal":0,"url":"http:\/\/cp9.serverse.com:2199\/rpc"}]

I used this code to post "artist" name of "Unkwown Artist" to my text field but it didn't work for me.

JSONObject parentObject = new JSONObject(finalJson);
                JSONArray parentArray = parentObject.getJSONArray("data");
                JSONObject finalObject = parentArray.getJSONObject(0);
                String songName = finalObject.getString("artist");
                return songName;

2 Answers 2

1

track is a jsonobject containing artist and track is inside first jsonobject of data array so fetch track then fetch artist from it

String songName = finalObject.getJSONObject("track").getString("artist");

{
    "type":"result",
    "data":[                            // fetch JSONArray
        {                               // fetch first JSONObject
             "title":"My Stream ","song":"Unknown - The Authorised One",
             "track":{                  // fetch track JSONObject
                  "artist":"Unknown .." // fetch string 
Sign up to request clarification or add additional context in comments.

1 Comment

i am glad that i could help , stay safe out there :)
0

Have you tried the jackson parser? It's super easy to use and can easily parse the string above. All you need to do is create 3/4 POJO classes that map your structure and then apply the readValue function of the Mapper to the outer class. Following is a small example with a List and one inner class:

import java.io.IOException;
import java.util.List;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;

class OutterPojo {

    public String aString;
    public List<InnerPojo> aList;

    public String getaString() {
        return aString;
    }

    public void setaString(String aString) {
        this.aString = aString;
    }

    public List<InnerPojo> getaList() {
        return aList;
    }

    public void setaList(List<InnerPojo> aList) {
        this.aList = aList;
    }

    @Override
    public String toString() {
        return new StringBuilder().append("{ #OutterPojo# ").append("aString:").append(aString).append(", ").append("aList:")
                .append(aList).append(" }").toString();
    }
}

class InnerPojo {

    public String anotherString;

    public String getanotherString() {
        return anotherString;
    }

    public void setanotherString(String anotherString) {
        this.anotherString = anotherString;
    }

    @Override
    public String toString() {
        return new StringBuilder().append("{ #InnerPojo# ").append("anotherString:").append(anotherString).append(" }")
                .toString();
    }
}

public class Test {

    public static void main(String args[]) throws JsonParseException, JsonMappingException, IOException {
        String jsonData = "{\"aString\":\"s\",\"aList\":[{\"anotherString\":\"ss\"},{\"anotherString\":\"sss\"}]}";
        ObjectMapper objectMapper = new ObjectMapper();
        OutterPojo testObject = objectMapper.readValue(jsonData, OutterPojo.class);
        System.out.println(testObject);
    }

}

As to dependencies all you need for this to work is jackson-core, jackson-databind and jackson-annotations - here's the link to maven: https://mvnrepository.com/artifact/com.fasterxml.jackson.core

Hope it helps! :)

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.