3

This is followup of a previous question (Android: Json string with spaces gives "Unterminated object at" exception)

When I get a string from a resource file it removes the " unless it is escaped (\") is there a way around this?

For example:

Java:

String jString = getResources().getString(R.string.event);

Json:

<resources>
    <string name="event">
        {"Array":[{"Name":"One two three"},{"Name":"Two"},{"Name":"Three"}]}
    </string>
</resources>

jString has a value of {Array:[{Name:One two three},{Name:Two},{Name:Three}]} and I want it to have the following value {"Array":[{"Name":"One two three"},{"Name":"Two"},{"Name":"Three"}]}.

Community
  • 1
  • 1
Andrew Boes
  • 2,013
  • 4
  • 22
  • 29
  • You answered the question yourself: "unless it is escaped (\") is there a way around this?" What's the problem with that? – Julian Sep 07 '11 at 03:22
  • The problem is that I get the string from a server that serializes an object and I don't want to write my own serializer. I think it's kind of silly that it removes the quotes from a string and am hoping there is away around it. – Andrew Boes Sep 07 '11 at 03:27
  • When are you fetching this JSON obect? At (or before) compile? This doesn't really make sense to me at the moment. – Julian Sep 07 '11 at 03:32
  • I'm fetching it before compile. – Andrew Boes Sep 07 '11 at 03:38

1 Answers1

3

You can place the JSON document in res/raw/ and then use openRawResource(int) to get an InputStream from which you can read the JSON.

But depending on the data, I'd prefer saving it in a <string-array> or something like that.

Julian
  • 2,051
  • 2
  • 22
  • 30
  • Is there any way to make [ButterKnife](http://jakewharton.github.io/butterknife/) bind the raw resource? Writing [10+ lines of code](http://stackoverflow.com/a/6349913/5922757) just to read a string seems like a little overkill. – Jezor Aug 11 '16 at 02:00