Попробуйте это:
$categoryArray = [];
foreach($pricing->categories as $category) {
$categoryResult['categoryCode'] = $category->categoryCode;
$categoryResult['CategoryDescription'] = $category->category_description;
$categoryResult['Products'] = [];
foreach($category->products as $product) {
$productResult['Product_code'] = ''; // this doesn't appear in your JSON...
$productResult['CountNumber'] = $product->productInfo->productType->quantDetails;
$productResult['ProductDescription'] = $product->productInfo->productType->desc;
$productResult['price_amount'] = $product->pricing->MainPrice;
$categoryResult['Products'][] = $productResult;
}
$categoryArray[] = $categoryResult;
}
$priceResult = $categoryArray; // ($priceResult is a strange name for this array...)
Use %
encoding for the ^
character, viz. http://finance.yahoo.com/q/h?s=%5EIXIC
You need to encode the URI to replace illegal characters with legal encoded characters. If you first make a URL (so you don't have to do the parsing yourself) and then make a URI using the five-argument constructor, then the constructor will do the encoding for you.
import java.net.*;
public class Test {
public static void main(String[] args) {
String myURL = "http://finance.yahoo.com/q/h?s=^IXIC";
try {
URL url = new URL(myURL);
String nullFragment = null;
URI uri = new URI(url.getProtocol(), url.getHost(), url.getPath(), url.getQuery(), nullFragment);
System.out.println("URI " + uri.toString() + " is OK");
} catch (MalformedURLException e) {
System.out.println("URL " + myURL + " is a malformed URL");
} catch (URISyntaxException e) {
System.out.println("URI " + myURL + " is a malformed URL");
}
}
}
Вы должны закодировать свои параметры.
Что-то вроде этого будет делать:
import java.net.*;
import java.io.*;
public class EncodeParameter {
public static void main( String [] args ) throws URISyntaxException ,
UnsupportedEncodingException {
String myQuery = "^IXIC";
URI uri = new URI( String.format(
"http://finance.yahoo.com/q/h?s=%s",
URLEncoder.encode( myQuery , "UTF8" ) ) );
System.out.println( uri );
}
}
http: // java .sun.com / javase / 6 / docs / api / java / net / URLEncoder.html
Пространство кодируется к %20 в URL, и к + в формах отправил данные (тип контента application/x-www-form-urlencoded). Вам нужен первый.
Используя Гуаву:
dependencies {
compile 'com.google.guava:guava:28.1-jre'
}
можно использовать UrlEscapers:
String encodedString = UrlEscapers.urlFragmentEscaper().escape(inputString);
не используют String.replace, это только закодировало бы пространство. Пользуйтесь библиотекой вместо этого.