Search code examples
androidjacksondollar-sign

How to parse JSON graph with special character annotation using Json Jackson?


here is my graph "seriesType": { "code": "83001", "$": "Série" } How do I parse this json with json jackson? I am not able to because the dollar sign is generating an error.

i tried this

public Dollar serieType;
public class Dollar {
public int code;
public String $;

}

// is is the input stream
jp = jsonFactory.createJsonParser(is);
        serieType = objectMapper.readValue(jp, Dollar.class);
 strBuffer.append("serieType: " + dollar.$ + "\n");

// It's some how what i tried, i am sure that the problem is caused by the dollar sign.

I tried to get a value like this String s=tvseries.serieType.$ and this is the stacktrace 01-02 10:48:03.905: W/ActivityManager(58): Launch timeout has expired, giving up wake lock! 01-02 10:48:04.623: W/ActivityManager(58): Activity idle timeout for HistoryRecord{5b799e28 com.test.jackson/.MainAlloCine} 01-02 10:48:07.886: W/System.err(275): java.lang.NullPointerException 01-02 10:48:07.895: W/System.err(275): at com.test.jackson.MainAlloCine.onCreate(MainAlloCine.java:49) 01-02 10:48:07.895: W/System.err(275): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 01-02 10:48:07.895: W/System.err(275): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627) 01-02 10:48:07.895: W/System.err(275): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) 01-02 10:48:07.895: W/System.err(275): at android.app.ActivityThread.access$2300(ActivityThread.java:125) 01-02 10:48:07.895: W/System.err(275): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033) 01-02 10:48:07.895: W/System.err(275): at android.os.Handler.dispatchMessage(Handler.java:99) 01-02 10:48:07.895: W/System.err(275): at android.os.Looper.loop(Looper.java:123) 01-02 10:48:07.895: W/System.err(275): at android.app.ActivityThread.main(ActivityThread.java:4627) 01-02 10:48:07.895: W/System.err(275): at java.lang.reflect.Method.invokeNative(Native Method) 01-02 10:48:07.895: W/System.err(275): at java.lang.reflect.Method.invoke(Method.java:521) 01-02 10:48:07.905: W/System.err(275): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 01-02 10:48:07.905: W/System.err(275): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 01-02 10:48:07.905: W/System.err(275): at dalvik.system.NativeStart.main(Native Method)


Solution

  • Just add @JsonProperty annotation:

    @JsonProperty("$")
    public String value; // or whatever name you want
    

    assuming your problem is with Java compiler which does not let you use dollar character in names (because it uses it internally how inner class names etc).

    EDIT:

    Looking at exact JSON, the other potential issue is simple: your structure does not match. You need one wrapper object:

    public class DollarWrapper {
        public Dollar seriesType
    }
    

    and bind to that. But as usual, this extra level of wrapping seems... well, extra. Why does service insist on adding it?