0

So I'm attempting to create a JSON object from this string since I will be accessing the iTunes lookup api, but I can't seem to create the JSONObject. I've looked at every android/java based solution and I can't seem to get it work. Thanks!

import org.json.JSONException;
import org.json.JSONObject;

public class api_caller {

static String api_out = "{\"wrapperType\":\"track\", \"kind\":\"podcast\", \"collectionId\":274450056, \"trackId\":274450056, \"artistName\":\"Giant Bomb\"}";

static String fin = api_out.replace("\\", "");

public static void main(String[] args) {
    System.out.println(fin);
    try {
        JSONObject jsondata = new JSONObject(fin);
    }catch(JSONException e){
        e.printStackTrace();
    }
}

}

EDIT:

So I've tried what Darshan mentioned. But I have been consistently getting this error.

/Library/Java/JavaVirtualMachines/jdk1.8.0_31.jdk/Contents/Home/bin/java -Didea.launcher.port=7540 "-Didea.launcher.bin.path=/Applications/Android Studio.app/Contents/bin" -Dfile.encoding=UTF-8 -classpath "/Users/jsho/Library/Android/sdk/platforms/android-23/android.jar:/Users/jsho/Library/Android/sdk/platforms/android-23/data/res:/Users/jsho/projects/simple_podcasts/app/build/intermediates/classes/debug:/Users/jsho/projects/simple_podcasts/app/build/intermediates/exploded-aar/com.android.support/animated-vector-drawable/23.2.1/res:/Users/jsho/projects/simple_podcasts/app/build/intermediates/exploded-aar/com.android.support/animated-vector-drawable/23.2.1/jars/classes.jar:/Users/jsho/projects/simple_podcasts/app/build/intermediates/exploded-aar/com.android.support/support-v4/23.2.1/jars/classes.jar:/Users/jsho/projects/simple_podcasts/app/build/intermediates/exploded-aar/com.android.support/support-v4/23.2.1/res:/Users/jsho/projects/simple_podcasts/app/build/intermediates/exploded-aar/com.android.support/support-v4/23.2.1/jars/libs/internal_impl-23.2.1.jar:/Users/jsho/projects/simple_podcasts/app/build/intermediates/exploded-aar/com.android.support/recyclerview-v7/23.2.1/jars/classes.jar:/Users/jsho/projects/simple_podcasts/app/build/intermediates/exploded-aar/com.android.support/recyclerview-v7/23.2.1/res:/Users/jsho/Library/Android/sdk/extras/android/m2repository/com/android/support/support-annotations/23.2.1/support-annotations-23.2.1.jar:/Users/jsho/projects/simple_podcasts/app/build/intermediates/exploded-aar/com.android.support/support-vector-drawable/23.2.1/res:/Users/jsho/projects/simple_podcasts/app/build/intermediates/exploded-aar/com.android.support/support-vector-drawable/23.2.1/jars/classes.jar:/Users/jsho/projects/simple_podcasts/app/build/intermediates/exploded-aar/com.android.support/design/23.2.1/jars/classes.jar:/Users/jsho/projects/simple_podcasts/app/build/intermediates/exploded-aar/com.android.support/design/23.2.1/res:/Users/jsho/projects/simple_podcasts/app/build/intermediates/exploded-aar/com.android.support/appcompat-v7/23.2.1/jars/classes.jar:/Users/jsho/projects/simple_podcasts/app/build/intermediates/exploded-aar/com.android.support/appcompat-v7/23.2.1/res:/Applications/Android Studio.app/Contents/lib/idea_rt.jar" com.intellij.rt.execution.application.AppMain com.example.jsho.simplepod.api_caller
Exception in thread "main" java.lang.RuntimeException: Stub!
    at org.json.JSONObject.<init>(JSONObject.java:7)
{"wrapperType":"track", "kind":"podcast", "collectionId":274450056, "trackId":274450056, "artistName":"Giant Bomb"}
    at com.example.jsho.simplepod.api_caller.main(api_caller.java:20)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)

Process finished with exit code 1
tachyonflux
  • 20,103
  • 7
  • 48
  • 67

1 Answers1

1

There is no need of string.replace() operation, the quotes are escaped already. Following should work fine:

System.out.println(api_out);
try {
    JSONObject jsondata = new JSONObject(api_out);
    System.out.println(jsondata);
} catch (JSONException e) {
    e.printStackTrace();
}
Darshan Mehta
  • 30,102
  • 11
  • 68
  • 102